基本Java错误
好的,我的代码如下基本Java错误,java,Java,好的,我的代码如下 //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
//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");
}
我只是想知道为什么当我运行它时,我必须立即输入我的猜测,而没有它运行欢迎消息,它只在我按enter键后运行,然后告诉我我是正确的还是错误的,认为这与我有关。下一行但我不确定?您需要移动代码:
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();
把它移到这里来
//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);
这是因为您有以下内容:String userGuess=keyboard.nextLine().toUpperCase();在收到你的欢迎信之前,非常感谢!所以只需使用keyboard.nextLine();在实际的节目中?是的。nextLine()需要您按enter键才能继续。只要这一行在欢迎词之前,就必须首先处理这个问题