Results 1 to 5 of 5
- 09-26-2013, 12:21 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 18
- Rep Power
- 0
having some issues with nested loops. keeps resetting values and wont work properly.
i know im missing something here and well, i THINK this is what he expects us to do for this program. i'm not sure though, however im running into an issue where the total deposit resets to 1000. earlier on, i had it where if i put -1 to exit, it did but if i typed in 200 for withdraw, it went into an infinite loop.
any ideas?
Java Code:import java.util.Scanner; public class BankAccount { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner in= new Scanner(System.in); // reads in user input String accountStatus="Active"; //state of activity for account double acctBalance= 1000; //fixed account balance double withdrawAmt = 0; // user input to amount withdrawn double newBalance = acctBalance-withdrawAmt; // for the new balance after subtracting the withdraw from the account balance do{ System.out.print ("enter withdrawl amount. type -1 to exit: "); withdrawAmt= in.nextDouble(); newBalance--; newBalance= acctBalance-withdrawAmt; //new account balance after withdraw if (accountStatus.equals("Active")&&(acctBalance>=withdrawAmt)){/* checking to see if account is active and withdraw ammount is equal to or less than the current balance */ System.out.println("your new balance is : "+ newBalance); // If true, this shows the new balance in the account } else{ System.out.println("insufficient funds"); // prints if the withdraw amount is greater then the account balance. } } while (newBalance >=0|| withdrawAmt!=-1); System.out.println("your new balance is : "+ newBalance); } }// end main
- 09-26-2013, 03:12 AM #2
Member
- Join Date
- Oct 2012
- Posts
- 18
- Rep Power
- 0
Re: having some issues with nested loops. keeps resetting values and wont work proper
it keeps either looping back to the original balance of 1000 instead of decreasing each time i withdraw. it also doesnt exit if i type -1 and keeps adding 1 to the balance.
any pointers?
- 09-26-2013, 04:43 AM #3
Re: having some issues with nested loops. keeps resetting values and wont work proper
When using an OR with multiple conditions, if ANY of the sub conditions are true, the whole expression is true.
If you used an AND, then ALL of the conditions would have to be true for the whole expression to be true.
Look at the conditions tested in the while().If you don't understand my response, don't ignore it, ask a question.
- 09-26-2013, 05:31 AM #4
Member
- Join Date
- Oct 2012
- Posts
- 18
- Rep Power
- 0
Re: having some issues with nested loops. keeps resetting values and wont work proper
i removed the || and simply made it !=-1, but it keeps adding 1 and not deducting.
i was testing it so i can get to $0 and get the insufficient funds message, but it only restarts to 1000. even if i put -1, it doesnt kick out.
- 09-26-2013, 05:44 AM #5
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: having some issues with nested loops. keeps resetting values and wont work proper
There are several problems. First, why do you have a newBalance variable? You don't need it from what I can tell. Remove the newBalance-- statement and change all the other newBalance variables to acctBalance.
The other problem is that when you use -1 to signal the end of the session, you process it as a valid amount. It doesn't hit the while condition till the end. So you need to check for -1 as soon as it is entered and take action immediately.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads
-
n Nested Loops
By joyful in forum New To JavaReplies: 6Last Post: 10-16-2012, 05:14 AM -
[Semi-Beginner] (nested loops) What's wrong with my code? (nested loops)
By Solarsonic in forum New To JavaReplies: 20Last Post: 03-22-2011, 05:02 AM -
Nested Loops
By candygirl198827 in forum New To JavaReplies: 38Last Post: 12-01-2010, 07:03 AM -
class that wont work out properly.
By vampire-elf in forum New To JavaReplies: 7Last Post: 09-07-2010, 02:39 AM -
Why wont my object render properly
By toecutter in forum AWT / SwingReplies: 3Last Post: 10-22-2009, 02:43 PM
Bookmarks