gpt4 book ai didi

java - 退款 FOR 循环无法正常工作

转载 作者:行者123 更新时间:2023-12-01 16:36:20 28 4
gpt4 key购买 nike

我被这个 Java 家庭作业问题困扰:

Write a new method, Refund, that simulates the refunding of any remaining credits in multiples of 10 by repeatedly printing out the balance and decrementing by 10 each time until there is less than 10 credits remaining. Then begin decrement the balance by 1 credit repeatedly whilst printing to screen until the balance is zero. For example, if the current balance is 33 and Refund is called, the output to screen will look like this:

Balance: 33
Balance: 23
Balance: 13
Balance: 3
Balance: 2
Balance: 1
Balance: 0

它适用于任何数字,除了以“0”结尾的数字,即 10,30,100 等。

这是我的退款方法:

public void Refund(){
System.out.println("You have selected the refund option:");

for(int counter=(int)balance;counter>=10;counter-=10){
System.out.println("Balance: £"+balance);
balance-=10;
}

for(int counter=(int)balance;counter>0;counter-=1){
System.out.println("Balance: £"+balance);
balance-=1;
}

System.out.println("Balance: £"+balance);
}

基本上,我可以让它处理 10,30 等数字的唯一方法就是这样做

for(int counter=(int)balance-10;counter>=10;counter-=10){

以下声明(退款方式):

for(int counter=(int)balance;counter>=10;counter-=10){
System.out.println("Balance: £"+balance);
balance-=10;
}

但是,现在我已经这样做了,10,30,100 可以工作,但任何其他数字,即 33,54,62 等都不起作用!有什么建议吗?

最佳答案

尝试这样的事情。 (未测试)

while(balance != 0) {
System.out.println("The balance: " + balance);
if(balance >= 10)
balance -= 10;
else
balance--;
}

关于java - 退款 FOR 循环无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8638882/

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