gpt4 book ai didi

java - 如果 switch 中默认输入会忽略 IF 语句

转载 作者:行者123 更新时间:2023-12-01 09:02:13 25 4
gpt4 key购买 nike

我正在做一个项目(基于教程)。我有一个 switch 语句,对于每种情况,都有一个默认值,以防用户输入无效,我在控制台上写“抱歉,我不明白您的请求”。但是,如果用户不写任何内容,而是写“退出”,则程序应该结束,而不会出现“我不明白请求”的句子。

这在我开头的 IF 语句中已说明。当我输入“exit”时,我当前的项目所做的就是显示该行然后停止。我不明白程序如何在开始时完全忽略该 IF 语句。

public class MainGame {

public static GameSave gameSave = new GameSave();
public static String user = "";
public static Scanner scanner = new Scanner(System.in);
public static String question;
public static int relationshipPoints;

public static void main(String[] args) {
Random random = new Random();
question = gameSave.loadGame();
// relationshipPoints = gameSave.loadPoints();
RelationshipPoints points = new RelationshipPoints();

System.out.println("\n\t*** TEXT_GAME: FIRSTDATE ***\n");
System.out.println("-You can exit the game at any time by typing 'exit'.-\n\n");

while (true) {

if (user.equalsIgnoreCase("exit")) {
System.exit(1);
break;
} else {
switch (question) {
[...]
case "2":
switch (user = scanner.next()) {
case "1":
System.out.println("\n\nThe guy you met last night was nice. You want to "
+ "get back into contact with him. Why don't you check your phone for a number?");
question = "2A";
gameSave.saveGame("2A");
break;
case "2":
System.out.println("\n\n");
question = "0";
break;
default:
System.out.println("\nI do not understand your request.\n");
question = "2";
break;
}
break;
case "2A": [...]

最佳答案

尝试将 while(true) {...} 替换为 while ((user = Scanner.next() != null) { ... }

看起来您正在尝试访问“用户”数据而不先进行设置。

关于java - 如果 switch 中默认输入会忽略 IF 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41577669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com