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

时间:2013-02-13 20:34:22

标签: java if-statement while-loop joptionpane

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”语句为false并且可以继续其他声明。

1 个答案:

答案 0 :(得分:0)

只需在真实条件下添加 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;
        }
    }
}