0

快速提问。我想知道如何做到这一点,以便它仅在用户输入 2-23 的数字时运行。

         BufferedReader in;
         int x;
         String playerx;

         //user input for number of players
         in = new BufferedReader(new InputStreamReader(System.in));
         System.out.println("\nWelcome to the Casino!");
         System.out.println("How many players are playing? (2-23)");
         //string for number of players
         playerx = in.readLine(); 
         //convert players to integer
         x = Integer.valueOf(playerx).intValue(); 

         //condition player x so that the number of players are between 2 and 23
         if(playerx.compareTo("1")==0 && playerx.compareTo("24")==0) {
            System.out.println("\nSorry, there are either not enough players or not enough cards for that option.");
        }else { 
4

2 回答 2

2

由于您已经int对玩家数量具有价值,即x,因此更好地进行比较以使事情变得简单。

改变这个:

     if(playerx.compareTo("1")==0 && playerx.compareTo("24")==0) {

     if(x > 1 && x > 24) {
于 2013-10-07T03:34:41.610 回答
0

尝试

  if( x< 2 ||  x >  23) {
            System.out.println("\nSorry, there are either not enough players or not enough cards for that option.");
   }else { 
于 2013-10-07T03:37:42.593 回答