gpt4 book ai didi

java - 我该如何摆脱这个怪圈

转载 作者:行者123 更新时间:2023-12-04 23:09:47 25 4
gpt4 key购买 nike

我知道当状态为真时循环将继续运行。我想如果我简单地输入 loop=false,在循环的 } 括号之后我可以继续编码。显然我错了,它不会运行任何东西。请有人告诉我如何摆脱这个 hell 般的 while 循环。

System.out.println("You total balance is 0.00, "
+ "please deposit coins and type done when finished");

while(loop){
if (input.hasNextInt()){
deposit = input.nextBigDecimal();}
String change = input.next();

switch (change){

case "quarter":
balance= quarter.multiply(deposit);
total=total.add(balance);
System.out.println("Your balance is "+ total +" :Make addiontal deposit(s)");
break;

case "dime":
balance=dime.multiply(deposit);
total=total.add(balance);
System.out.println("Your balance is "+ total +" :Make addiontal deposit(s)");
break;
case "nickel":
balance=nickel.multiply(deposit);
total=total.add(balance);
System.out.println("Your balance is "+ total +" :Make addiontal deposit(s)");
break;
case "penny":
balance=penny.multiply(deposit);
total=total.add(balance);
System.out.println("Your balance is "+ total +" :Make addiontal deposit(s)");
break;
case"done":
System.out.println("Your total is $"+total);
fee=total.multiply(feeRate);
System.out.println("The exchance fee is 9.1% which amounts to $"+fee);
grandTotal=total.subtract(fee);
System.out.println("Your total balance minus the exchange fee is $"+grandTotal);

break;

default: System.out.println("There is a issue at "+change);}

} System.out.println("4");

}

}

最佳答案

关键是更改循环变量,使其变为假,从而让您退出 while 循环。因此,更改 case "done" block 中的循环变量。在该 block 中,设置 loop = false;

    case"done":
System.out.println("Your total is $"+total);
fee=total.multiply(feeRate);
System.out.println("The exchance fee is 9.1% which amounts to $"+fee);
grandTotal=total.subtract(fee);
System.out.println("Your total balance minus the exchange fee is $"+grandTotal);

loop = false; // ************ add this ************

break;

请记住,关键概念是导致循环继续的事情必须以某种方式循环本身内进行更改。否则 while 循环永远不会结束。

关于java - 我该如何摆脱这个怪圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26444423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com