退款FOR循环不能正常工作

时间:2011-12-26 20:23:01

标签: java loops for-loop

我坚持使用这个Java作业问题:

  

编写一种新方法,退款,模拟通过重复打印出余额并每次减少10来减少10的倍数的任何剩余信用的退款,直到剩余少于10个信用。然后在打印到屏幕时重复开始减少1个信用额度,直到余额为零。例如,如果当前余额为33并且调用了Refund,则输出到屏幕将如下所示:

     

余额:33
  余额:23
  余额:13
  余额:3
  余额:2
  余额:1
  余额: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语句中(退款方法):

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

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

4 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

这应该有效:

int balance = 33;

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

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

        }

答案 2 :(得分:1)

只是为了澄清这个问题。 您的退款方法可以正常使用。

它适合你的约束“每次递减10次,直到小于10 剩余的积分”。
如果将约束更改为小于或等于10 ,则不适合。

这是我的测试,有效:

public class Main {

    public static void main(String[] args) {
        Main m = new Main();
        m.refund(33);
    }

    public void refund(float balance) {
        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);
    }
}

我没有修改您方法的正文。

以下是30输入的输出:

You have selected the refund option:
Balance: £30.0
Balance: £20.0
Balance: £10.0
Balance: £0.0

在33:

You have selected the refund option:
Balance: £33.0
Balance: £23.0
Balance: £13.0
Balance: £3.0
Balance: £2.0
Balance: £1.0
Balance: £0.0

答案 3 :(得分:1)

这里回答这个问题的方法很多......

//Using While loops
public void refund(int balance){
    while(balance >= 10) {
        System.out.println("Balance: £"+balance);
        balance -= 10;
    }
    while(balance >= 0) {
        System.out.println("Balance: £"+balance);
        balance -= 1;
    }
}
// another possible while loop answer and my favorite
public static void refund(int balance){
    while(balance >= 0) {
        int decrement = (balance>=10?10:1);
        System.out.println("Balance: "+balance);
        balance -= decrement;
    }
}

// Using for loops
public void refundWithForLoops(int balance){
    for(balance=balance; balance >= 10; balance -=10) {
        System.out.println("Balance: £"+balance);
    }
    for(balance=balance; balance >= 0; balance--) {
        System.out.println("Balance: £"+balance);
    }
}

// with recursion
public static void refundWithRecursion(int balance){
    int decrement = (balance>=10?10:1);
    System.out.println("Balance: £"+balance);
    if(balance > 0) refundWithRecursion(balance-decrement);
}