我写了这个叫做足球队烘焙师的程序。它存储球员的球衣号码和球员评分。然后在菜单选项中执行用户想要做的事情。但由于某种原因,菜单选项继续打印两次。我被告知它是因为当我要求PlayerRating[i]
的int输入时,当我第一次要求用户输入String“menuOption”时,它会接受Enter按钮和输入。有人可以帮忙吗?
这是代码
import java.util.Scanner;
public class SoccerTeamRoaster {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
final int JERSY_NUMS = 5;
int[] JersyNumber = new int[JERSY_NUMS];
int[] PlayerRating = new int[JERSY_NUMS];
String[] jPut = new String[JERSY_NUMS];
int i = 0;
boolean quit = false;
for (i = 0; i < JERSY_NUMS; i++) {
System.out.println("Enter player " + (i + 1) + "'s jersey number: ");
JersyNumber[i] = sc.nextInt();
System.out.println("Enter player " + (i + 1) + "'s rating: ");
PlayerRating[i] = sc.nextInt();
System.out.println("");
}
System.out.println("");
System.out.println("ROSTER");
int N = 0;
for (N = 0; N < JERSY_NUMS; N++) {
System.out.println("Player " + (N + 1) + " -- Jersey number: " + JersyNumber[N] + ", Rating: " + PlayerRating[N]);
}
while (!quit) {
String Stg = "MENU\n"
+ "u - Update player rating\n"
+ "a - Output players above a rating\n"
+ "r - Replace player\n"
+ "o - Output roster\n"
+ "q - Quit\n";
System.out.println("");
System.out.println(Stg);
System.out.println("Choose an option: ");
String menuOption = "?";
menuOption = sc.nextLine();
boolean correctInput = false;
if (menuOption.equals("u") || menuOption.equals("a") || menuOption.equals("r") || menuOption.equals("o") || menuOption.equals("q")) {
correctInput = true;
menuOption = menuOption.trim();
} else {
correctInput = false;
}
if (menuOption.equals("u")) {
System.out.println("Enter jersey number: ");
int jerseyNum = sc.nextInt();
System.out.println("New rating for player: ");
int newRate = sc.nextInt();
int M = 0;
for (M = 0; M < JERSY_NUMS; M++) {
if (JersyNumber[M] == jerseyNum) {
PlayerRating[M] = newRate;
}
}
} else if (menuOption.equals("a")) {
System.out.println("Enter a rating: ");
int rating = sc.nextInt();
int k = 0;
for (k = 0; k < JERSY_NUMS; k++) {
if (PlayerRating[k] > rating) {
System.out.println("Player " + (k + 1) + " -- Jersey Number: " + JersyNumber[k] + ", Rating: " + PlayerRating[k]);
}
}
} else if (menuOption.equals("o")) {
System.out.println("ROSTER");
int J = 0;
for (J = 0; J < JERSY_NUMS; J++) {
System.out.println("Player " + (J + 1) + " -- Jersey number: " + JersyNumber[J] + ", Rating: " + PlayerRating[J]);
}
} else if (menuOption.equals("q")) {
quit = true;
} else if (menuOption.equals("r")) {
System.out.println("Enter jersey number: ");
int jerNum = sc.nextInt();
int l = 0;
for (l = 0; l < JERSY_NUMS; l++) {
if (JersyNumber[l] == jerNum) {
System.out.println("Enter new jersey number: ");
JersyNumber[l] = sc.nextInt();
System.out.println("Enter new player rating: ");
PlayerRating[l] = sc.nextInt();
}
}
int a = 0;
for (a = 0; a < JERSY_NUMS; a++) {
System.out.println("Player " + (a + 1) + " -- Jersey number: " + JersyNumber[a] + ", Rating: " + PlayerRating[a]);
}
}
}
return;
}
}
答案 0 :(得分:0)
它打印两次的原因是因为这段代码...
menuOption = sc.nextLine();
将其更改为
menuOption = sc.next();
现在,它只打印一次
答案 1 :(得分:0)
您使用Scanner#nextInt
获取球衣号码和费率。但是Scanner#nextInt
不消耗输入的最后一个换行符,因此在此之后调用Scanner#nextLine时会消耗该换行符。
所以你必须调用Scanner#nextLine
一次,以便在while循环之前抛出换行符。