簡體   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