好吧,我正在嘗試製作一個程序,僅顯示5位學生排名前2的最高得分者。Java最高分
所以示例輸出。
Enter your name : Spear
Enter score : 56
Enter your name : Sky
Enter score : 61
Enter your name : Spy
Enter score : 45
Enter your name : Raks
Enter score : 31
Enter your name : Felicio
Enter score : 39
Congratulations Sky!
Congratulations Spear!
我只知道如何取最大的分數,而不是第二個這裏是我到目前爲止。
import java.util.Scanner;
public class Highest{
public static void main(String[]args) {
Scanner x = new Scanner(System.in);
String name = "";
int score;
int k;
int highest = 0;
num = x.nextLine();
largest = num;
for (int i = 0; i <= 5; i++) {
System.out.print("Enter name: ");
k = x.nextInt();
System.out.print("Enter score: ");
num = x.nextInt();
if (num > highest) {
highest = num;
}
}
System.out.println(largest);
}
} // how do i display the name of the highest score and the second placer?
我如何打印他們的名字? – Jake
我想我有一個邏輯錯誤在我的循環如何保持他們的名字。 – Jake
我應該爲名稱創建一個數組嗎?因爲當我輸入名字,然後我去鍵入第二我不能。 – Jake