简体   繁体   English

如果不执行do while循环条件,如何打印消息

[英]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 我将如何打印一条消息,指出“错误:您必须输入0到5之间的数字”,然后允许用户再次输入

 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. 我使用正则表达式来防止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. 它将避免大多数答案的解决方案提供的number >= 0 && number <= 5的重新计算。


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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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