有木有人可不可以解答一下我的问题😭
非常感谢!
题目:
错误答案:
结果:15.0
问题:不明白为什么不进入if循环,在cash大于50000算完之后,是不是没有更新cash的最新数据,还是原始100000代入if里,所以判断为真,不进入if循环了
有木有人可不可以解答一下我的问题😭
非常感谢!
题目:
public class Apple{
public static void main(String[] args){
double cash = 100000;
double chance = 0;
double tax = 0.05;
while(cash > 0){
if(cash > 50000){
cash = cash - cash * tax;
chance++;
}else if(cash >= 1000 && cash <= 50000){
cash = cash - 1000;
chance++;
}else{
break;
}
}
System.out.println(chance);
}
}
if 语句被移出 while 循环,并且在 if 语句中使用了一个 else if 语句来避免重复执行 if 语句。当 cash 的值小于等于 0 时,while 循环会结束,程序会打印出 chance 的值,即该人可以经过的路口数。