考试成绩如果用外部输入的话,代码怎么实现
就是成绩用Scanner输入的
就是成绩用Scanner输入的
2016-12-11
import java.util.*; public class HelloWorld { //完成 main 方法 public static void main(String[] args) { Scanner sc = new Scanner(System.in); // ArrayList<Integer> input = new ArrayList<>(); System.out.println("How many scores: "); if (sc.hasNextInt()){ int number = sc.nextInt(); int[] inputScore = new int[number]; for (int i = 0; i < number; i ++){ System.out.println("Input score:"); inputScore[i] = sc.nextInt(); } } top3(inputScore); // while (sc.hasNextInt()){ // input.add(sc.nextInt()); // } // int[] scores = new int[]{89 , -23 , 64 , 91 , 119 , 52 , 73}; // top3(scores); } //定义方法完成成绩排序并输出前三名的功能 public static int[] top3(int[] scores){ int[] result = new int[3]; int count = 0; Arrays.sort(scores); for (int i = scores.length - 1; count < 3 && i >= 0; i --){ if (scores[i] >= 0 && scores[i] <= 100){ result[count++] = scores[i]; } } System.out.println(Arrays.toString(result)); return result; }
举报