我有一个程序,允许用户输入他们希望分配成绩的学生数量的数字。然后,它打印出哪个学生不在总数中(例如6个中的1个),然后提示用户输入他们的名字和分数。
我正常工作的分数但是当显示最终消息时,我无法获得最大名称和第二个最大名称来显示任何内容。
Enter the number of students:2
Student 1 of 2
Enter Student's name:Noah
Enter Student's score:100
Student 2 of 2
Enter Student's name:Ryan
Enter Student's score:50
The highest score was 100.0 and got it
The second highest score was 50.0 and got it
有人可以告诉我为什么会这样吗?
public class TwoHighestScores {
public static void main(String[] args) {
//Declare variables
// Initialize Scanner
Scanner stdin = new Scanner(System.in);
// Enter the number of students
System.out.print("Enter the number of students:");
int n = stdin.nextInt();
double MaxValue = 0.0;
double secondMax = 0.0;
String MaxName = " ";
String secondMaxName = " ";
int count = 1;
for (int i = 0; i < n; i++) {
System.out.println("Student " + count + " of " + n);
System.out.print("Enter Student's name:");
String name = stdin.nextLine();
stdin.nextLine();
System.out.print(name);
count++;
System.out.print("Enter Student's score:");
double score = stdin.nextDouble();
// check
if (score > MaxValue) {
secondMax = MaxValue;
MaxName = name;
secondMaxName = MaxName;
MaxValue = score;
} else if (score > secondMax) {
secondMax = score;
secondMaxName = name;
}
}
System.out.println("The highest score was " + MaxValue + " and " + MaxName + " got it");
System.out.println("The second highest score was " + secondMax + " and " + secondMaxName + " got it");
}
}
答案 0 :(得分:1)
几个问题。
1)else-if不是必需的。你可以(现在已经)&#34;降级&#34;第一个if语句中的当前最大值到第二个max。
2)你做这个操作的顺序是错误的。这里的评论突出了这个问题。您已通过相等的传递属性将secondMaxName
设置为name
。
if (score > MaxValue) {
secondMax = MaxValue;
MaxName = name; // Sets MaxName = name
secondMaxName = MaxName; // secondMaxName = MaxName = name
MaxValue = score;
}
您可以通过简单的重新排序来解决这个问题。
if (score > MaxValue) {
// Demote the current max
secondMax = MaxValue;
secondMaxName = MaxName;
// Assign the new max
MaxName = name;
MaxValue = score;
}
3)初学者错误,但您在使用扫描仪时没有正常工作,正如我在评论中所讨论的那样。
您是否注意到您正在进行System.out.println(name);
,但该名称未被打印?这是因为String name = stdin.nextLine();
实际上是在先前输入的数字后读取换行符,然后以下stdin.nextLine()
实际读取您输入的名称。
所以,你需要像这样补偿新线
System.out.print("Enter the number of students: ");
int n = stdin.nextInt();
double MaxValue = 0.0;
double secondMax = 0.0;
String MaxName = " ";
String secondMaxName = " ";
for (int i = 1; i <= n; i++) {
stdin.nextLine(); // Clear the newline from the Scanner
System.out.println("Student " + i + " of " + n);
System.out.print("Enter Student's name:");
String name = stdin.nextLine();
System.out.print("Enter Student's score:");
double score = stdin.nextDouble();
// check
if (score > MaxValue) {
// Demote the current max
secondMax = MaxValue;
secondMaxName = MaxName;
// Assign the new max
MaxName = name;
MaxValue = score;
}
}
System.out.println("The highest score was " + MaxValue + " and " + MaxName + " got it");
System.out.println("The second highest score was " + secondMax + " and " + secondMaxName + " got it");
答案 1 :(得分:0)
首先,始终将最大值分配给Integer.MIN_VALUE
,然后才进行比较。
您的If语句中存在问题
if (score > MaxValue){
secondMax = MaxValue;
***MaxName = name;
secondMaxName = MaxName;***
MaxValue = score;
}
交换这两行并且它应该工作:你一直用MaxName覆盖secondMaxName。将其更改为
if (score > MaxValue){
secondMax = MaxValue;
secondMaxName = MaxName;
MaxName = name;
MaxValue = score;
}
代码中的逻辑错误如下:
如果学生1是sree并且得分是20:如果条件 MaxName = Sree和secondMaxName = sree ..这适用于第一次迭代
迭代2:学生2是saha得分是40:在你的if条件下 MaxName是Saha,现在您将MaxName分配给secondMaxName,后者也变为Saha而不是sree
清楚?