简体   繁体   中英

How to print a message if the do while loop condition is not met

How would I print a message saying "Error: you have to enter a number between 0 and 5", then allowing the user to input again

 int number;
 do 
  {
     String textinput = JOptionPane.showInputDialog("give me a number between 0 and 5");
     number = Integer.parseInt(textinput);
  } while (!(number >= 0 && number <= 5));

The simplest method which alters your original code the least is as follows:

int number;
do {
    String textinput = JOptionPane.showInputDialog("give me a number between 0 and 5");
    number = Integer.parseInt(textinput);
    if((number < 0) || (number > 5) {
        //show error message
        continue;  //continue isn't absolutely necessary here, but perhaps for readability
    }
} while (!(number >= 0 && number <= 5));

Although I find this a little clunky and redundant, you're essentially checking the same condition twice. I'd go with a method more like the following:

int number;
String textinput = JOptionPane.showInputDialog("give me a number between 0 and 5");
while(true) {
    number = Integer.parseInt(textinput);
    if((number >= 0 && number <= 5)) {
        //show error message and prompt for another input
        contine; //As with before, continue isn't necessary here, but could add readability
    } else /*input was good*/ { break; /*exit while loop*/ }
}

you could do it like this. I used a regular expression to prevent a NumberFormatExcption.

int number = -1;
do 
{
   String textinput = JOptionPane.showInputDialog("give me a number between 0 and 5");
   if( textinput.match("0*[0-5]") {
     number = Integer.parseInt(textinput);
   }
   else {
     System.out.println("Error");
   }
} while (!(number >= 0 && number <= 5));
    String textinput = JOptionPane.showInputDialog("give me a number between 0 and 5");
    int number = Integer.parseInt(textinput);

    while (!(number >= 0 && number <= 5)) {
        textinput = JOptionPane.showInputDialog("Your number must be between 0 and 5!");
        number = Integer.parseInt(textinput);
    }

    // do stuff

I believe this would be the least redundant and most readable way to write this:

int number;
while ((number = requestNumber()) < 0 || number > 5) {
    // Show your prompt
}

Declare this method somewhere:

private int requestNumber() {
    try {
        String textinput = JOptionPane.showInputDialog("give me a number between 0 and 5");
        return Integer.parseInt(textinput); 
    } catch(NumberFormatException nfe) {
        return -1;
    }
}

This is another option:

boolean valid;
int number;
do {
    String textinput = JOptionPane.showInputDialog("give me a number between 0 and 5");
    number = Integer.parseInt(textinput);
    if (number >= 0 && number <= 5) {
        valid = true;
    } else {
        // Show your message here
        valid = false;
    }
} while (!valid);

It would avoid the recalculation of number >= 0 && number <= 5 offered by the solutions of most of the answers.


The code in this answer was not tested

        int number;
        if(nummber>0 && number<5)
        {
         do 
          {
             String textinput = JOptionPane.showInputDialog("give me a number between 0 and 5");
             number = Integer.parseInt(textinput);
          } while (!(number >= 0 && number <= 5));
        }
    else
    {
    System.out.println("you have to enter a number between 0 to 5");
}
int number;
Boolean bolContinue = true;
 do 
  {
     String textinput = JOptionPane.showInputDialog("give me a number between 0 and 5");
     number = Integer.parseInt(textinput);
     if (!(number >= 0 && number <= 5)) {
         //Log here
         bolContinue = false;
     }
  } while (bolContinue);

It should be as simple as that.

int number;
do 
{
 String textinput = JOptionPane.showInputDialog("give me a number between 0 and 5");
 number = Integer.parseInt(textinput);
 if(!(number >= 0 && number <= 5))
     {
       System.out.println("Error: you have to enter a number between 0 and 5");
     }
} while (!(number >= 0 && number <= 5));

Like this:

int number;
     do 
      {
         String textinput = JOptionPane.showInputDialog("give me a number between 0 and 5");
         number = Integer.parseInt(textinput);
         if ( number > 5 || number  < 0)
         {
             System.out.println("Please enter number between 0 to 5");
             return;
         }
          } while (!(number >= 0 && number <= 5));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM