在while循环中请求用户输入两次



我目前正在做一个java项目。该项目涉及一个关于NHL统计的数据库,以及通过用户输入访问数据库中的信息。我使用while循环在多种情况下请求用户输入,例如请求收到的点球数、进球数、积分等。我在某个特定部分遇到困难,它会请求用户输入他们想要的统计数据(得分、进球、助攻、罚点球、助攻、球员、俱乐部(。之后,如果用户键入玩家作为他们的选择,那么系统应该询问用户他们希望查看哪些玩家的统计数据。问题是,由于我在用户第一次输入后使用while循环,因此在玩家第二次输入之前,循环将被执行,程序将结束。任何帮助都将不胜感激,这是我的代码:

import java.util.Scanner;
import nhlstats.NHLStatistics;
public class NhlStatisticsPart2 {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("NHL statistics service");
while (true) {
System.out.println("");
System.out.print("command (points, goals, assists, penalties, player, club, quit): ");
String command = reader.nextLine();
if (command.equals("quit")) {
break;
}
if (command.equals("points")) {
NHLStatistics.sortByPoints();
NHLStatistics.top(10);
// Print the top ten players sorted by points.
} else if (command.equals("goals")) {
NHLStatistics.sortByGoals();
NHLStatistics.top(10);
// Print the top ten players sorted by goals.
} else if (command.equals("assists")) {
NHLStatistics.sortByAssists();
NHLStatistics.top(10);
//  Print the top ten players sorted by assists.
} else if (command.equals("penalties")) {
NHLStatistics.sortByPenalties();
NHLStatistics.top(10);
//  Print the top ten players sorted by penalties.
} else if (command.equals("player")) {
System.out.println("Which player statistics?");
NHLStatistics.searchByPlayer(command);

// Ask the user first which player's statistics are needed and then print them.
} else if (command.equals("club")) {
// Ask the user first which club's statistics are needed and then print them.
// Note: When printing statistics they should be ordered by points (so the players with the most points come first).
}
}
}
}

这是我收到的信息:

command (points, goals, assists, penalties, player, club, quit): player
Which player statistics?
command (points, goals, assists, penalties, player, club, quit):

可以看出,该程序在不允许用户输入的情况下返回到前面的问题,而不是允许用户输入他们想要查看的玩家的统计数据。有没有一种方法,用户可以在不执行循环的情况下回答问题(哪个玩家的统计数据?(?

您需要要求用户输入播放器。因此,您需要使用扫描仪扫描另一个值。如果您在If条件下执行此操作,则在用户输入值之前,它不会继续。

.....
} else if (command.equals("player")) {
System.out.println("Which player statistics?");
command = reader.nextLine();  //Read again from the input
NHLStatistics.searchByPlayer(command);  //now use that second value
}
.......

相关内容

  • 没有找到相关文章

最新更新