Java 找到最高分
我编写了一个程序,提示用户输入学生的姓名和分数。最后显示得分最高的学生 这是我的代码,我应该怎么做Java 找到最高分,java,Java,我编写了一个程序,提示用户输入学生的姓名和分数。最后显示得分最高的学生 这是我的代码,我应该怎么做 public class Student { public static void main(String[] args) { Scanner input = new Scanner(System.in); final int numberOfStudent = 10; int count = 0; int highestS
public class Student {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
final int numberOfStudent = 10;
int count = 0;
int highestScore = 0;
String highestScorer = "";
String x= "";
int y;
while (count < numberOfStudent) {
System.out.println("Enter student's name and score.");
x = input.next();
y = input.nextInt();
count++;
if (highestScore < y){
highestScore = y;
highestScorer = x;
}
}
System.out.println(highestScore +" "+ highestScorer);
}
}
公共班级学生{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
最终整数学生=10;
整数计数=0;
int最高分=0;
字符串最高分=”;
字符串x=“”;
int-y;
while(count
好吧,最后,你的x
包含最后一个学生,而不是得分最高的学生
正如保留highestScore
变量一样,您也应该保留highestScoreUser
变量。无论何时更新highestScore
,您也应该更新highestScoreUser
if (highestScore < y){
highestScore = y;
highestScoreUser = x;
}
if(最高分
您也可以使用Math.max(highestScore,y)
来保持所需的最大值,而不是使用如果我应该怎么做:试着编译并运行它,看看它是否工作正常?问题是什么,问题到底出在哪里?我知道,但我不知道如何才能找到得分最高的学生。@ZpCikTi我只是建议如何做。如果超过1名学生有相同的高分怎么办?