Domanda

Quick question. I'm wondering how I can make this so that it will only run when a number from 2-23 is entered by the user.

         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 { 
È stato utile?

Soluzione 2

try

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

Altri suggerimenti

As you already have int value for number of players i.e x, so better that for comparison to keep things simple.

Change this :

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

to

     if(x > 1 && x > 24) {
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top