英文:
Switch statement in loop with unknown number of repetitions
问题
以下是翻译好的代码部分:
String[] action = {"x", "u", "d", "l", "r", "s", "h", "e"};
System.out.println("请输入一个动作");
String input = sc.next();
char selection;
do {
selection = input.charAt(0);
switch (selection) {
case 'x': System.out.print("再见!");
break;
case 'u': System.out.print("你向上走了一格。");
break;
case 'd': System.out.print("你向下走了一格。");
break;
case 'l': System.out.print("你向左走了一格。");
break;
case 'r': System.out.print("你向右走了一格。");
break;
case 's': System.out.print("你搜索了这个区域,但什么都没找到。");
break;
case 'h': System.out.print("你躲藏起来,等待敌人的出现。大约一个半小时后变得无聊,于是你放弃了。");
break;
case 'e': System.out.print("你吃了一些食物。你恢复了0点生命值。");
break;
case 'z':
break;
default:
System.out.println("我不明白你的意思。");
break;
}
} while (selection != 'z');
注意:在翻译过程中,我保留了代码的逻辑结构和标识符(如变量名、方法名等),只对注释进行了翻译。
英文:
Hi I am doing a little task where I need to build a loop with unknown number of repetitions. I have a problem with my code where when the user enters in a character the output displays infinitely. How do I fix this?
String[] action = {"x", "u", "d", "l", "r", "s", "h", "e"};
System.out.println("Please enter an action");
String input = sc.next();
char selection;
do{
selection = input.charAt(0);
switch(selection){
case 'x': System.out.print("Bye!");
break;
case 'u' : System.out.print("You go one square up.");
break;
case 'd': System.out.print("You go one square down.");
break;
case 'l': System.out.print("You go one square left.");
break;
case 'r': System.out.print("You go one square right.");
break;
case 's': System.out.print("You search the square for treasure. You find nothing.");
break;
case 'h': System.out.print("You hide, waiting for enemies to come by. It gets boring after about an hour and a half, so you give up.");
break;
case 'e': System.out.print("You eat some food. You regain 0 hit points");
break;
case 'z':
break;
default:
System.out.println("I dont understand");
break;
}
}
while (selection != 'z');
```
</details>
# 答案1
**得分**: 0
`sc.next()` 会暂停执行,直到用户输入内容。 <br/>
但由于它位于循环外部,它不会停下来读取新的输入。<br/>
因此,你需要将 `next()` 调用移到循环内部。
```java
do {
String input = sc.next();
selection = input.charAt(0);
switch(selection){
// 其余的代码
}
英文:
sc.next()
halts the execution until user does an input. <br/>
However since it is outside the loop, it will not stop to read a new input.<br/>
Therefore you need to move the next()
call into the loop.
do{
String input = sc.next();
selection = input.charAt(0);
switch(selection){
// rest of the code
}
答案2
得分: 0
你需要将用户的输入也放入do-while循环中。这样它会在每次迭代之后询问新的输入。我现在在用手机,所以不能给你展示。但这是一个简单的修复。
英文:
You need to put the users input into the do-while loop as well. So it asks for new input after every iteration. I am currently on my phone so I can not show you. But its an easy fix.
答案3
得分: 0
首先,您需要逐个字符地读取输入。由于您已经输入了一个单词,所以需要从索引0开始,在do-while循环中继续移动到下一个字符。为此:
String[] action = {"x", "u", "d", "l", "r", "s", "h", "e"};
System.out.println("Please enter an action:");
Scanner sc = new Scanner(System.in);
String input = sc.next();
char selection;
int i = 0;
do {
selection = input.charAt(i++);
switch (selection) {
case 'x':
System.out.print("Bye!");
break;
case 'u':
System.out.print("You go one square up.");
break;
case 'd':
System.out.print("You go one square down.");
break;
case 'l':
System.out.print("You go one square left.");
break;
case 'r':
System.out.print("You go one square right.");
break;
case 's':
System.out.print("You search the square for treasure. You find nothing.");
break;
case 'h':
System.out.print("You hide, waiting for enemies to come by. It gets boring after about an hour and a half, so you give up.");
break;
case 'e':
System.out.print("You eat some food. You regain 0 hit points");
break;
case 'z':
break;
default:
System.out.println("I dont understand");
break;
}
} while (i < input.length() && selection != 'z');
您需要在while()
内部加入另一个条件,以避免超出输入字符串的大小。此外,我无法理解为什么您需要在开头创建的action
字符串数组。
英文:
First of all, you need to read the input character-by-character. Since you already took a word input, you need to start from index 0 and continue moving to the next character in the do-while loop. For that:
String[] action = {"x", "u", "d", "l", "r", "s", "h", "e"};
System.out.println("Please enter an action:");
Scanner sc = new Scanner(System.in);
String input = sc.next();
char selection;
int i = 0;
do{
selection = input.charAt(i++);
switch(selection){
case 'x': System.out.print("Bye!");
break;
case 'u' : System.out.print("You go one square up.");
break;
case 'd': System.out.print("You go one square down.");
break;
case 'l': System.out.print("You go one square left.");
break;
case 'r': System.out.print("You go one square right.");
break;
case 's': System.out.print("You search the square for treasure. You find nothing.");
break;
case 'h': System.out.print("You hide, waiting for enemies to come by. It gets boring after about an hour and a half, so you give up.");
break;
case 'e': System.out.print("You eat some food. You regain 0 hit points");
break;
case 'z':
break;
default:
System.out.println("I dont understand");
break;
}
} while (i < input.length() && selection != 'z');
You need to have another condition inside while()
to avoid exceeding the size of the input string. Also, I couldn't understand why you need the String array action
that you created in the beginning.
专注分享java语言的经验与见解,让所有开发者获益!
评论