2013-10-07 70 views
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 { 

回答

0

嘗試

if(x< 2 || x > 23) { 
      System.out.println("\nSorry, there are either not enough players or not enough cards for that option."); 
    }else { 
+0

是的,這工作非常感謝你! –

+0

我有點空白......「||」的意思是「或」對嗎? –

+0

是的,這是OR條件 – upog

2

正如你已經擁有玩家數量int值即x,所以最好是比較讓事情變得簡單。

更改此:

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

 if(x > 1 && x > 24) { 
+0

但我想X是大於1且小於24他們不能等於1或24也是。這會工作嗎? 'if(x <= 1 && x > = 24){' –

+0

@ZackMortimer條件適用於x大於1且小於24(不包括1和24) –