繁体   English   中英

如何实现循环以使程序再次要求用户输入而不是结束程序

[英]How do I emplement a loop to get the program to ask the user for input again instead of ending the program

ans = JOptionPane.showInputDialog(null, "Enter amount to be withdrawn from checking");
double withCheck = Double.parseDouble (ans);
if (withCheck >= checking)
    {
    JOptionPane.showMessageDialog(null, "You do not have enough in your account for that.");
    }
else
    {
    double sum = checking - withCheck;
    JOptionPane.showMessageDialog(null, "Your checking balance is now: $" + sum + ", and your savings balance is: $" + savings);
    }
}

当前,这段代码在withCheck> = check时结束程序,我想知道一种使用某种循环再次询问该问题的方法,直到“ if”语句为假并可以继续else语句为止。

只需添加一个带有真实条件的while循环 ,并在某个条件变为真实时break它。

While(true)
{
    ans = JOptionPane.showInputDialog(null, "Enter amount to be withdrawn from checking");
    double withCheck = Double.parseDouble (ans);
    if (withCheck >= checking)
        {
        JOptionPane.showMessageDialog(null, "You do not have enough in your account for that.");
        }
    else
        {
        double sum = checking - withCheck;
        JOptionPane.showMessageDialog(null, "Your checking balance is now: $" + sum + ", and your savings balance is: $" + savings);
        break;
        }
    }
}

暂无
暂无

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

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