我需要能够接受用户输入,直到输入大于初始价格,但我还需要使其健壮,以便用户不能通过输入除double/integer之外的其他内容来破坏程序.如果用户输入的内容不是double/int.
问题是它创建了一个循环并重复"请输入有效货币"+"请输入:价格"
public static double findChange()
{
System.out.println("\nPlease insert: " + price + " (enter payment amount)");
initialPrice = price;
while (payment < price)
{
try{
payment = kb.nextDouble();
}
catch (Exception e)
{
System.out.println("Please enter valid currency");
}
if (payment > 0){
totalPayment += payment;
price -= payment;
price = (price * 100);
payment = 0;
}
if (totalPayment < initialPrice)
System.out.println("Please Insert:" + price);
}
change = totalPayment - initialPrice;
change = Math.round(change * 100);
change = change …Run Code Online (Sandbox Code Playgroud)