繁体   English   中英

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

[英]JOptionPane.showInputDialog loop (using do while loop)

我正试图要求用户提供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()

我从main调用它如下:

public static void main(String[] args) {

UserInput getResult = new UserInput();
int company_size =  getResult.companySize();

}// ends main

如果用户第一次写错了值,我不确定为什么你需要两个相同的对话框,因为最后只返回一个值(ansCompany)。

通过将do-while语句设置为break条件(小于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;

}

while ( ansCompany < 4 || ansCompany > 10); 是低于3 高于11的任何东西

你想要的while ( ansCompany >= 4 && ansCompany <= 10);

注意 :你想要这个选择^^^^的原因是因为如果输入大于>或等于=使> =比4和更高的意思。 同样地,小于<或等于=使<=比意味着小于10的任何东西

|| 是指OR 这意味着输入必须大于4或小于10.如果答案是11,则它传递第一个条件,因此传递if语句。 与3相同,它通过小于10的条件。

&&表示AND ,因此必须通过第一个条件和第二个条件。

从这个意义上说,你的if语句也是错误的;

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

应该

if ( ansCompany >= 4 && ansCompany <= 10 ) {

我宁愿使用递归函数:

版本1:简而言之:

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

版本2:但您也可以使用静态常量

/* 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();
}

版本3:如果您想显示错误消息:

/* 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();
}

暂无
暂无

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

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