Tic Tac Toe Java输入字母而非数字时用户输入无效

ix0qys7i  于 2023-01-04  发布在  Java
关注(0)|答案(3)|浏览(170)

当用户输入字母而不是数字时,将发生InputMistmatchException错误,程序将退出。
我怎样才能改变它,使程序将打印“无效输入,请输入一个数字”,而不是直接退出?除了使用catch之外,还有其他方法吗?我在网上找到了这个方法,但我认为我的老师不会允许。
这是我的主类,如果它帮助

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
  TicTacToe game = new TicTacToe();
  Scanner scanner = new Scanner(System.in);
  
    game.printBoard();
    System.out.println(game.getDirections());
    
    // Keep playing the game until it is over
    while (!game.checkWin()) {
      // Read the next move from the user
      int move = scanner.nextInt();
      
      // Make the move and update the game state
      boolean spaceNotOccupied = game.checkInput(move);
            if (spaceNotOccupied) {
                game.makeMove(move);
            }
      
      // Check if the game is over
      if (game.checkWin()) {
        game.printResults();
        return;
      }
      
      // Change the current player and print the updated game board
      game.changePlayer();
      game.printBoard();
      System.out.println(game.getDirections());
    }
    
    // Print the final result of the game
    game.printResults();
    
    }
  }

这是makeMove方法,它显示要输入的数字

public void makeMove(int space) {
  // Update the game board
    if (space == 1) {
    s1 = turn;
  } else if (space == 2) {
    s2 = turn;
  } else if (space == 3) {
    s3 = turn;
  } else if (space == 4) {
    s4 = turn;
  } else if (space == 5) {
    s5 = turn;
  } else if (space == 6) {
    s6 = turn;
  } else if (space == 7) {
    s7 = turn;
  } else if (space == 8) {
    s8 = turn;
  } else if (space == 9) {
    s9 = turn;
  } else {
      System.out.println("Invalid input, please try again.");
      return;
    }
  }
iq0todco

iq0todco1#

不要直接假设(使用)输入为Integer,首先验证它是否为Integer或not。如果不是,循环直到得到一个。

// Read the next move from the user    
System.out.print("Please provide your next move");
while (!scan.hasNextInt()) {
     System.out.print("Invalid input, please enter a number");
     scan.next();
}
     
int move = scan.nextInt();
ctrmrzij

ctrmrzij2#

您可以使用字符串输入,而不是假设输入表示整数,然后检查该字符串是否由数字等组成。
这不是你的问题,但是当用户输入一个已经被占据的方块时,你的代码仍然会改变玩家。你应该只在实际移动时才改变玩家。
以下是建议的代码(我没有测试,因为我没有您的其余代码):

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        TicTacToe game = new TicTacToe();
        
        // Keep playing the game until it is over
        while (true) {
            game.printBoard();
            System.out.println(game.getDirections());
            
            // Read the next move from the user as a string
            String moveString = scanner.nextLine();
            if (moveString.matches("[1-9]")) { // Is it a single digit between 1 and 9?
                int move = Integer.valueOf(moveString); // Convert to integer
                if (game.checkInput(move)) { // Is the square free?
                    game.makeMove(move);
                    if (game.checkWin()) break;
                    game.changePlayer();
                } else {
                    System.out.println("That square is already occupied.");
                }
            } else {
                System.out.println("Please enter a digit between 1 and 9.");
            }
        }
        game.printResults();
        scanner.close();
    }
fwzugrvs

fwzugrvs3#

您可以使用无限循环来实现这一点,但是您需要检查用户是否对每个有效输入都获胜。

boolean playing = true;
while(playing){
   switch(space){
      case 1:
           //code for doing operation
           //winning check
           break;
      ...
      default:
           System.out.println("Invalid Input");
   }          
}

相关问题