验证用户输入井字游戏

时间:2019-12-15 03:15:47

标签: java validation if-statement user-input tic-tac-toe

我需要有关此分配的帮助,我不确定如何修复我的代码。我要确保如果用户输入的数字不是数字,它将提示“ Invalid entry!”。

if(checkPositionAvailability(move))
               {
                   board[move] = 'H'; //make 'H' for player move
                   break;
               }
               else
               {
                   System.out.println("Position not available.\nMake a different choice.");
                   continue;
               }
           }
           else
           {
               System.out.println("Invalid entry!");
               continue;
           }
       }

1 个答案:

答案 0 :(得分:0)

public class HelloWorld{

     public static void main(String []args){

        char c = 'c'; 
        char one = '1'; 
        char two = '2'; 
        System.out.printf("Is available [%c] ? %s\n", c, IsAvailable(c));
        // Is available [c] ? false
        System.out.printf("Is available [%c]  ? %s\n",one, IsAvailable(one));
        // Is available [c] ? true
        System.out.printf("Is available [%c]  ? %s\n",two, IsAvailable(two));
        // Is available [c] ? true
     }

     public static boolean IsAvailable(char character){
         String s = Character.toString(character); 
         try {
             int num = Integer.parseInt(s);
             return true; 
         }catch (NumberFormatException e){
             // means it is not a number. Thus return false
             return false;
         }
     }
}