【发布时间】:2020-08-18 02:05:28
【问题描述】:
当用户的输入无效时,我正在尝试进行 while 循环,这意味着它不是“是”或“否”,这样问题就会再次出现。如果你能在这里帮助我,我将不胜感激!!!谢谢(我已经删除了大部分代码,除了问题,因为它太长了!!) 代码:
import java.util.Scanner;
import java.util.Random;
public class ShowWhatYouKnow {
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
Random rd = new Random();
String ready;
int guess;
int[] dice = {1, 2, 3, 4, 5, 6};
System.out.println("Welcome to dice game!");
System.out.println("");
System.out.println("Guess a number between 1-6 and win $50!!");
System.out.println("Are you ready??");
ready = key.nextLine();
// trying to do a do while here so if the user inputs the invalid option this would accur again and again
/*
System.out.println("invalid input !");
System.out.println("");
System.out.println("Guess a number between 1-6 and win $50!!");
System.out.println("Are you ready??");
ready = key.nextLine();
*/
if (ready.equalsIgnoreCase("yes")) {
System.out.println("Let The Game Begin!!");
System.out.println("-----------------------------------------");
}
} else if(ready.equalsIgnoreCase("no")){
System.out.println("You Exited The Program!! Bye!");
System.out.println("-----------------------------------------");
System.out.println("");
}
}
}
【问题讨论】:
-
也许看看添加另一个?这看起来有点像 UNI/学校的作业。
标签: javascript java arrays loops while-loop