好的,所以我的代码如下;
//Variables
Scanner keyboard = new Scanner(System.in);
String userGuess = keyboard.nextLine().toUpperCase();
final String correctAnswer = "RBR";
//Mastermind program
//Welcome message
System.out.println("\nHello " +/*username*/" and welcome to Mastermind!\n");
System.out.println("##########################################################################\n");
System.out.println("In this game you have 3 guesses to guess the order of a sequence of letters," +
"\nthe sequence will consist of the letters R and/or B, in a random order" +
"\nand you have to guess the order that they are in." +
"\nYou will be awarded points depending on how many guesses it takes you to" +
"\nguess the sequence and how many letters you get correct.\n");
System.out.println("##########################################################################\n");
//Game start
System.out.println("Please enter your first guess: " + userGuess);
if(userGuess.equals(correctAnswer)){
System.out.println("Congratulations you got it in one go!");
}
else{
System.out.println("Sorry try again");
}
我只是想知道为什么当我运行它时我必须立即输入我的猜测,没有它运行欢迎信息,它只在我按下回车后运行但是然后要么告诉我我是正确的还是不正确的,想想它与.nextLine有关,但我不确定?
答案 0 :(得分:1)
将它移到这里
//Game start
System.out.println("Please enter your first guess:");
Scanner keyboard = new Scanner(System.in);
String userGuess = keyboard.nextLine().toUpperCase();
System.out.println("Your guess is: " + userGuess);
答案 1 :(得分:0)
您需要移动代码:
String userGuess = keyboard.nextLine().toUpperCase();
之后的欢迎信息:
//Game start
System.out.println("Please enter your first guess: " + userGuess);
Scanner keyboard = new Scanner(System.in);
String userGuess = keyboard.nextLine().toUpperCase();