JAVA练习-创新互联
编写一个程序,提示用户输入学生数量后,再依次输入姓名和成绩,按照成绩的降序来打印学生的姓 名
当前标题:JAVA练习-创新互联
URL网址:http://azwzsj.com/article/cospgs.html
public class Test4 { // 编写一个程序,提示用户输入学生数量后,再依次输入姓名和成绩,按照成绩的降序来打印学生的姓 名 public static void main ( String [] args ) { System . out . println ( " 输入学生数量 " ); Scanner sc = new Scanner ( System . in ); int len = sc . nextInt (); String [] names = new String [ len ]; int [] scores = new int [ len ]; for ( int i = 0 ; i< names . length ; i ++ ) { names [ i ] = inputName ( sc ); scores [ i ] = inputScore ( sc ); } sort ( names , scores ); for ( int i = 0 ; i< names . length ; i ++ ) { System . out . println ( " 第 " + ( i + 1 ) + " 名学生: " + names [ i ] + ", 成绩 为 :" + scores [ i ]); } } public static String inputName ( Scanner sc ) { System . out . println ( " 姓名: " ); String tmp = "" ; while ( true ) { tmp = sc . next (); if ( tmp . length () >20 || tmp . length ()< 6 ) { System . out . println ( " 输入数据不合法,请重新输入 " ); } else break ; } return tmp ; } public static int inputScore ( Scanner sc ) { System . out . println ( " 成绩: " ); int tmp = 0 ; while ( true ) { tmp = sc . nextInt (); if ( tmp >100 || tmp< 0 ) { System . out . println ( " 输入数据不合法,请重新输入 " ); } else break ; } return tmp ; } public static void sort ( String [] names , int [] scores ) { for ( int i = 1 ; i< names . length ; i ++ ) { for ( int k = 0 ; k< names . length - i ; k ++ ) { if ( scores [ k ]< scores [ k + 1 ]) { int tmp = scores [ k ]; scores [ k ] = scores [ k + 1 ]; scores [ k + 1 ] = tmp ; String stmp = names [ k ]; names [ k ] = names [ k + 1 ]; names [ k + 1 ] = stmp ; } } } } }
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
创新互联-专业网站定制、快速模板网站建设、高性价比临海网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式临海网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖临海地区。费用合理售后完善,十年实体公司更值得信赖。当前标题:JAVA练习-创新互联
URL网址:http://azwzsj.com/article/cospgs.html