java - 如果在控制台中输入非数字,如何使程序继续运行

标签 java arrays graphics console java.util.scanner

这是一款四连子游戏,如果用户输入数字 1-7,则棋盘上会放置一个圆盘。如果数字不是1-7,则会弹出“重试”。但是,当用户输入字母或符号时,就会出现异常。 这是我的代码:

import java.util.*;
import java.awt.*;


public class ConnectFour{

   public static void main(String[] args){
      //board
      DrawingPanel panel = new DrawingPanel(550,550);
      int rowAvailable;
      Graphics g = panel.getGraphics();
      g.drawLine(0,0,0,427);
      g.drawLine(0,0,500,0);
      g.drawLine(500,0,500,427);
      g.drawLine(0,427,500,427);
      for(int i = 0; i< 6; i++){
         for(int j= 0; j<= 6; j++){
            g.setColor(Color.YELLOW);
            g.fillRect(j*71,i*71,71,71);
            g.setColor(Color.WHITE);
            g.fillOval(j*71,i*71,71,71); 
         }
      }

      //setBlankArray
      Scanner console = new Scanner(System.in);
      char[][] board = new char[6][7];
      for(int j = 0;j <= 6; j++){
         for(int i= 0; i < 6; i++){
            board[i][j] = ' ';
         }
      }
      boolean isBlack = true;
      boolean isRed = false;
      int column = 0;
      boolean playersTurn = true;
      boolean rightNum = false;

      //oneTurn
      while(getWinner(board, playersTurn)){
      //while(playersTurn == true){
         rightNum = false;
         if(isBlack == true){
            // displayCurrentPlayer
            System.out.println("Black's Turn");
            g.setColor(Color.WHITE);
            g.drawString("Red Disc's Turn",200, 450);
            g.setColor(Color.BLACK);
            g.drawString("Black Disc's Turn",200, 450);
         }
         else{
            // displayCurrentPlayer
            System.out.println("Red's Turn");
            g.setColor(Color.WHITE);
            g.drawString("Black Disc's Turn",200, 450);
            g.setColor(Color.RED);
            g.drawString("Red Disc's Turn",200, 450);
         }
         System.out.print("Choose a column to place your disk (1-7): ");
         while(rightNum == false){
            column = (console.nextInt()) -1;
            if(column >= 0 && column < 7 && board[0][column] == ' '){
               rightNum = true;
            }
            else{
               System.out.print("Try again: ");
            }
         }

         drawBoard(column, board, isBlack, isRed, board, g);
         isBlack = !isBlack;
      }
      if(isBlack == false){
         System.out.println("Congratulations Black Player");

      }
      else
         {System.out.println("Congratulations Red Player");
      }
      // use the while loop to say try again if the column is filled.
   }

   public static void drawBoard(int column, char[][] board, boolean isBlack, boolean isRed, char[][] availability,Graphics g){

      char player = ' ';
      if(isBlack == true){
         g.setColor(Color.BLACK);
         player = 'b';
      }
      else{
         g.setColor(Color.RED);
         player = 'r';
      }
      int x = 0;
      int row = 5;
      while(board[row-x][column] != ' '){
         x++;
      }
      row = row-x;
      g.fillOval((column * 71),(row * 71), 71,71);
      board[row][column] = player;
   }

   public static boolean getWinner(char[][] board, boolean playersTurn){
      int verticalCount = 0;
      boolean isVertical = false;
      for(int i = 0; i <= board[0].length - 1; i++){
         verticalCount = 0;                               
         for(int j = board.length - 1; j > 0; j--){
            if(board[j][i] == board[j-1][i] && board[j][i] != ' '){
               verticalCount++;
            } else {
               verticalCount = 0;
            }

            if(verticalCount == 3){
               isVertical = true;
            }
         }
      }

      int horizontalCount = 0;
      boolean isHorizontal = false;
      for(int i = board.length-1; i >= 0; i--){
         for(int j = 0 ; j < board[0].length-1; j++){
            if(board[i][j] == board[i][j+1] && board[i][j] != ' '){
               horizontalCount++;
            }
            else{
               verticalCount = 0;
            } 
            if(horizontalCount == 3){
               isHorizontal = true;
            }
         }
      }

      int diagonalCount = 0;
      boolean isDiagonal = false; 
     // for(int i = 0; i<=6; i++){
      //   for(int j =0; j<6; j++){
       //     if(board[i][j-1] == board[i][j]){
       //        diagonalCount++;
         //   } 
        // }
     // }

      if(isVertical || isHorizontal || isDiagonal){
         playersTurn = false;
      }
      else{
         playersTurn = true;}
      return playersTurn;
   }
}

最佳答案

当您使用console.nextInt()时,您的代码需要一个整数

使用 hasNextInt() 确定下一个内联字符是否为 int,如果不是,则使用 next() 跳过该字符

if(console.hasNextInt()){
     column = (console.nextInt()) -1;
     /* ... */
} else {
    console.next();
}

关于java - 如果在控制台中输入非数字,如何使程序继续运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30654015/

相关文章:

c++ - OpenGL 新手,正在处理 "paint"程序

java - 为什么 Android 不实习字符串资源?

java - 如何创建像 Clipboard Pro App 这样的 float 窗口?

java - 我如何对密码进行三层加密,SHA1,base64编码和盐

graphics - Collectd 权限被拒绝连接到远程主机

opengl - “Field of View”和 “lens Length”有什么关系

java - 我怎样才能让它变得更好

python - 在Python中构建由替代对象组成的矩阵

c - 使用 for 循环初始化一个数组,并将最终数组元素的值作为条件(在 C 中)

php - 根据列值对数据行进行分组