import java.util.Scanner;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
String answer;
//answer = input.nextLine();
do
{
//input.hasNextLine();
System.out.println("MAIN MENU");
System.out.println("- Start a new Game (S)");
System.out.println("- Exit (E)");
System.out.println("Please enter your choice: ");
answer = input.next();
if (answer.equals("s") || answer.equals("S"))
{
Hangman h1 = new Hangman();
h1.getWord();
h1.printData();
h1.CountTheLetters();
h1.GiveTheLetters();
}
input.hasNext();
} while (answer.equals("e") || answer.equals("E"));
System.out.println("Thank you for the game");
}
}
为什么这个循环只运行一次并且它不再要求新的入口?第一次出现的菜单我输入s并且我玩游戏。然后我想再次问我,但它没有做到。为什么? ......
答案 0 :(得分:2)
因为answer
不等于"e"
也不等于"E"
。此外,您需要检查扫描仪是否还有要读取的元素。
将行更改为:
} while (input.hasNext() && !answer.equals("e") && !answer.equals("E"));