Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JOptionPane.showInputDialog循环(使用do while循环)_Java_Loops_While Loop_Do While - Fatal编程技术网

Java JOptionPane.showInputDialog循环(使用do while循环)

Java JOptionPane.showInputDialog循环(使用do while循环),java,loops,while-loop,do-while,Java,Loops,While Loop,Do While,我正试图向用户请求一个介于4和10之间的整数。如果他们的回答超出这个范围,就会进入一个循环。当用户第一次正确输入数字时,它不会中断并继续执行else语句。如果用户在else语句中正确输入数字,它将正确中断 如果第一次输入正确,如何使其中断 **我是java的初学者,如果我在这里遗漏了一些愚蠢的东西,我向您道歉 public int companySize() { int ansCompany; do { ansCompany = Integer.parse

我正试图向用户请求一个介于4和10之间的整数。如果他们的回答超出这个范围,就会进入一个循环。当用户第一次正确输入数字时,它不会中断并继续执行else语句。如果用户在else语句中正确输入数字,它将正确中断

如果第一次输入正确,如何使其中断

**我是java的初学者,如果我在这里遗漏了一些愚蠢的东西,我向您道歉

public int companySize() {  

    int ansCompany;

    do {
        ansCompany = Integer.parseInt(JOptionPane.showInputDialog
                ("Please input the company size"));

        if ( ansCompany <= 4 && ansCompany <= 10 ) {
                         break;

        } else {
       ansCompany = Integer.parseInt(JOptionPane.showInputDialog
                ("Please enter a valid company size"));

        if ( ansCompany <= 4 && ansCompany <= 10 ) {
                          break;
        } // ends nested if condition
    } //ends else 
          }//ends do
    while ( ansCompany < 4 || ansCompany > 10);
    return ansCompany;
}// ends public int companySize()
while(ansCompany<4 | | ansCompany>10)是低于3或高于11的值


你想要
while(ansCompany>=4&&ansCompany或等于=使=大于等于4。同样地,对于小于的情况,我不确定如果用户第一次写错值,为什么需要两个相同的对话框,因为最终只会返回一个值(ansCompany)

通过将do while语句设置为中断条件(小于4或大于10),它将循环,直到用户输入正确的数字

public int companySize() {
 int ansCompany;

 do {
   ansCompany = Integer.parseInt(JOptionPane.showInputDialog
      ("Please input the company size"));

 } while (ansCompany < 4 || ansCompany > 10);

 return ansCompany;
public int companySize(){
国际安信公司;
做{
ansCompany=Integer.parseInt(JOptionPane.showInputDialog
(“请输入公司规模”);
}而(安信公司<4 | |安信公司>10);
返回公司;

}我宁愿使用递归函数:

版本1:简而言之:

public int companySize() {
    final int result = Integer.parseInt(JOptionPane.showInputDialog("Please input the company size"));
    return (result >= 4 && result <= 10) ? result : companySize();
}

如果我想让用户输入一个介于4和10之间的数字,而不是(ansCompany<4 | | ansCompany<10);如果输入的内容超出了这些限制,则需要一个循环?您能再解释一下我哪里出错了吗?我尝试了您的更改,但仍然无法使其正确中断。抱歉,我现在看到while和if语句哪里出错了。谢谢,我可以看出这是多么多余。我不确定为什么我认为我需要中断语句。但我可以n现在当然明白为什么它会用just do运行了。谢谢。我明白第二个对话框只有在我想告诉用户输入无效但不是必需的情况下才有用。没问题,在使用代码时要小心,不要处理如果一个人写非整数值会出现的异常。通过这个实现,prGram将崩溃!我还没有真正接触到任何捕获异常的东西,但我迫不及待地返回并实现它。
public int companySize() {
    final int result = Integer.parseInt(JOptionPane.showInputDialog("Please input the company size"));
    return (result >= 4 && result <= 10) ? result : companySize();
}
/* don't be afraid of shared constants; sometimes they're very "useful idiots" */
/*public*/ static final int MIN = 4;
/*public*/ static final int MAX = 10;
/*public*/ static final String MESSAGE = "Please input the company size";

public int companySize() {
    final int result = Integer.parseInt(JOptionPane.showInputDialog(MESSAGE));
    return (result >= MIN && result <= MAX) ? result : companySize();
}
/* don't be afraid of shared constants; sometimes they're very "useful idiots" */
/*public*/ static final int MIN = 4;
/*public*/ static final int MAX = 10;
/*public*/ static final String MESSAGE = "Please input the company size";
/*protected*/ static final String ERROR_MESSAGE = "Please input a valid company size";

public int companySize() {
    final int result = Integer.parseInt(JOptionPane.showInputDialog(MESSAGE));
    return (result >= MIN && result <= MAX) ? result : companySize(ERROR_MESSAGE);
}

private int companySize(String errorMessage) {
    final int result = Integer.parseInt(JOptionPane.showInputDialog(errorMessage));
    return (result >= MIN && result <= MAX) ? result : companySize(ERROR_MESSAGE);
}
public static void main(String[] args) {
    UserInput getResult = new UserInput();
    int company_size =  getResult.companySize();
}