嗨,我正在做一个小任务,我需要构建一个未知重复次数的循环。我的代码有问题,当用户输入字符时,输出将无限显示。我该如何解决?
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');
```
do{
String input = sc.next();
selection = input.charAt(0);
switch(selection){
// rest of the code
}