public static void main(String[] args) {
char play = 0;
System.out.println("Welcome to Tic-Tac-Toe!!");
System.out.print("Would you like to play a game? (enter 'y' for yes or 'n' for no): ");
play = keyboard.next().charAt(0);
if (play != 'y') {
System.out.println("Goodbye!");
}
playGame();
System.out.print("Would you like to play another game (enter 'y' for yes or 'n' for no): ");
play = keyboard.next().charAt(0);
if (play != 'y') {
System.out.println("Goodbye!");
}
}
// *******************************************************
private static void playGame() {
String move;
System.out.print("Who should move first? (c=computer h=human): ");
move = keyboard.nextLine();
move = move.toLowerCase();
while ( !move.equals("c") && !move.equals("h")) {
System.out.println("'" + move + "'"+ " is not a valid option.");
System.out.print("Who should move first? (c=computer h=human): ");
move =keyboard.nextLine();
move = move.toLowerCase();
}
System.out.print("The computer is X, the human is O");
if (move.equals("c")) {
char currentPlayer = 'c';
} else if (move.equals("h")) {
char currentPlayer = 'h';
}
char currentPlayer = ' ';
此方法询问应该先播放的用户(tic tac toe),然后键入" c"或" h"先发挥。如果输入任何其他内容,则它将循环,直到键入其中一个字符。我在上面的另一个方法中有其他代码,询问他们是否想要玩游戏,这会影响这种方法吗?
示例:
欢迎来到Tic-Tac-Toe !!
你想玩游戏吗? (输入' y表示是,或者' n'表示否):y谁先移动? (c =计算机h =人类):''不是一个有效的选择。 这就是问题
谁先移动? (c =计算机h =人类):c
计算机是X,人是O
答案 0 :(得分:-2)
这可能是因为您在输入任何内容之前都要进行检查。所以
"" (空字符串)!=" c"或" h"
您收到错误消息。