I’m stuck with this Java homework question:
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
It works perfectly fine with any number except numbers ending in ‘0’ i.e. 10,30,100 etc.
Here’s my Refund method:
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);
}
Basically, the only way I can make it work with 10,30 etc. numbers is to do
for(int counter=(int)balance-10;counter>=10;counter-=10){
in the following for statement (Refund method):
for(int counter=(int)balance;counter>=10;counter-=10){
System.out.println("Balance: £"+balance);
balance-=10;
}
However, now that I’ve done that, 10,30,100 work but any other numbers i.e. 33,54,62 etc. don’t work! Any suggestions?
Just to clarify the question. Your Refund method works fine.
It suits your constrain “decrementing by 10 each time until there is less than 10 credits remaining”.
It won’t suit if you change the constrain to is less or equal 10.
Here is my test, that works:
I haven’t modified the body of your method.
Here is the output on 30 input:
And on 33: