更新:想通了,代码已在下面修复。添加了一个 while 循环以确认也输入了 0 或更高的值。
所以我正在做一个作业,用户输入 8 个分数,你必须按照给出的顺序找到最高和最低分数以及它们的位置。我已经能够找到最高和最低的分数,但我不知道如何找到他们的位置。请帮我。到目前为止,这是我的代码。
import java.util.Scanner;
public class HighestLowestPoints {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int largest = -1;
int smallest = 0;
int highGame = 1;
int lowGame = 1;
for(int games = 1; games <= 8; games++) {
System.out.println("Please enter the Texans' score in game " + games);
int points = keyboard.nextInt();
while(points < 0) {
System.out.println("Please enter a value 0 or above");
points = keyboard.nextInt();
}
if(games == 1) {
smallest = points;
}
if(points > largest) {
largest = points;
highGame = games;
}
if(points < smallest) {
smallest = points;
lowGame = games;
}
if(games == 8){
System.out.println(largest + " in game " + highGame + "\n"
+ smallest + " in game " + lowGame);
}
}
}
}