我必须计算输入的 50 个成绩的最高和最低成绩,还要说谁有透视成绩。这是问题代码:
max=-999;
min=1000;
while(inFile.hasNext())
{
name = inFile.next();
grade = inFile.nextInt();
inFile.nextInt();
if(grade > max)
{
max = grade;
maxName = name;
}
if(grade < min)
{
min = grade;
minName = name;
}
System.out.println(minName + " has the lowest grade of " + min);
System.out.println(maxName + " has the highest grade of " + max);
}
我试着把System.out.println(minName + " has the lowest grade of " + min);
我while
loop
的,但它给了我错误:
H:\Java\Lab6.java:202: error: variable maxName might not have been initialized
System.out.println(maxName + " has the highest grade of " + max);
^
但是当我像这样放入.println
时if statements
:
if(grade > max)
{
max = grade;
maxName = name;
System.out.println(maxName + " has the highest grade of " + max);
}
if(grade < min)
{
min = grade;
minName = name;
System.out.println(minName + " has the lowest grade of " + min);
}
它给了我这个输出:
Robert has the highest grade of 70
Robert has the lowest grade of 70
Joel has the lowest grade of 64
Alice has the highest grade of 98
Larry has the lowest grade of 42
Christine has the lowest grade of 20
Alex has the lowest grade of 10
Mathew has the highest grade of 100
我想要的只是最后两个,因为它们是正确的。