Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,092 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,268 people online right now. Registration is fast and FREE... Join Now!




Looping Bank Options

 
Reply to this topicStart new topic

Looping Bank Options

HangTight
post 10 Oct, 2008 - 08:21 PM
Post #1


New D.I.C Head

*
Joined: 26 Sep, 2008
Posts: 14


My Contributions


I'm trying to make my program loop, that will allow users to go back to the main screen if they choose option 1, 2, or 3. And if you choose option 4 you just end the program. This is what I have so far, Im not even sure if Im using the right loop method.

CODE
import java.util.Scanner;

public class Bank11 {

    public static void main(String[] args) {

        
        Scanner scan = new Scanner(System.in);
        
      do {    

        double balance = 3500;
        
        
        double amount;
        
        
        System.out.println ("1.Dept");
        System.out.println ("2.Withdrawal");
        System.out.println ("3.Your Balance");
        System.out.println ("4.Exit");
        int num = scan.nextInt();
        
        if(num == 1)
       {
        System.out.println ("Deposit Amount?");
        amount = scan.nextDouble();
      
        
        startingBalance = startingBalance + amount;
        System.out.println("Balance IS");
        System.out.println(balance);
      
        }
        else if (num == 2)
        {
      
        System.out.println("Witdraw Amount?");
        amount = scan.nextDouble();
      
        
        startingBalance = startingBalance - amount;
        System.out.println("Balance is");
        System.out.println(balance);
        
        }
        else if (num == 3)
        {
        
    
        System.out.println("Your Balance");
        System.out.println(balance);
        
        }
        else if (num == 4)
        {
        

     } while (int = 1, 2, 3)
        
        System.out.println ("See Yea!");
            
    }

}
}
User is offlineProfile CardPM

Go to the top of the page

markhazlett9
post 10 Oct, 2008 - 09:24 PM
Post #2


D.I.C Head

**
Joined: 12 Jul, 2008
Posts: 132



Thanked 8 times
My Contributions


Hello

for one make sure that your while loop starts before your main menu, so that when the while loop loops, it will display the main menu. If you want it only to loop when the user enters options 1,2,3 you could use something like this...
CODE


while(UserInput != 4)
{

Your code here

}

This will allow everything to loop only when the user does not select option 4. Then if the user does choose option 4 then it will cancel out of the loop and continue with the rest of the code or end the program if there is none left... Hope this helps... let me know if there is anything else

User is offlineProfile CardPM

Go to the top of the page

Ephemeralz
post 10 Oct, 2008 - 10:12 PM
Post #3


New D.I.C Head

*
Joined: 11 Sep, 2008
Posts: 18



Thanked 1 times
My Contributions


QUOTE(HangTight @ 10 Oct, 2008 - 09:21 PM) *

I'm trying to make my program loop, that will allow users to go back to the main screen if they choose option 1, 2, or 3. And if you choose option 4 you just end the program. This is what I have so far, Im not even sure if Im using the right loop method.

CODE
import java.util.Scanner;

public class Bank11 {

    public static void main(String[] args) {

        
        Scanner scan = new Scanner(System.in);
        
      do {    

        double balance = 3500;
        
        
        double amount;
        
        
        System.out.println ("1.Dept");
        System.out.println ("2.Withdrawal");
        System.out.println ("3.Your Balance");
        System.out.println ("4.Exit");
        int num = scan.nextInt();
        
        if(num == 1)
       {
        System.out.println ("Deposit Amount?");
        amount = scan.nextDouble();
      
        
        startingBalance = startingBalance + amount;
        System.out.println("Balance IS");
        System.out.println(balance);
      
        }
        else if (num == 2)
        {
      
        System.out.println("Witdraw Amount?");
        amount = scan.nextDouble();
      
        
        startingBalance = startingBalance - amount;
        System.out.println("Balance is");
        System.out.println(balance);
        
        }
        else if (num == 3)
        {
        
    
        System.out.println("Your Balance");
        System.out.println(balance);
        
        }
        else if (num == 4)
        {
        

     } while (int = 1, 2, 3)
        
        System.out.println ("See Yea!");
            
    }

}
}



Change the while (int = 1, 2, 3) to while (num !=4)
That should work!

Edit: You should declare balance outside of the do while loop, or else your balance will reset every loop. Not a bad idea if you want to give me a million bucks more everytime I make a withdrawal! =p

Also, you may want to try using a switch statement instead of if-else. I find that easier to write and read too.



This post has been edited by Ephemeralz: 10 Oct, 2008 - 10:16 PM
User is offlineProfile CardPM

Go to the top of the page

stauffski
post 10 Oct, 2008 - 10:34 PM
Post #4


D.I.C Head

**
Joined: 3 Nov, 2007
Posts: 137



Thanked 16 times
My Contributions


Very close, just a small change. You want your loop to quit when num ='s 4, that also means you want it to keep running when it equals anything other than 4. So, just compare it to 4.

java

do{
//code here
}while(num != 4) ;//or inefficiently: while(num == 1 || num == 2 || num == 3);


That should do it for ya. Good Luck!
User is offlineProfile CardPM

Go to the top of the page

HangTight
post 11 Oct, 2008 - 04:48 AM
Post #5


New D.I.C Head

*
Joined: 26 Sep, 2008
Posts: 14


My Contributions


Thanks everyone for all the help!!! icon_up.gif icon_up.gif
But now I just have 1 problem. I get one error message satating "operator != cannot be applied to num,int" for the while part.



CODE
import java.util.Scanner;

public class Bank11 {

    public static void main(String[] args) {

        
        Scanner scan = new Scanner(System.in);
        
      do {    

        double balance = 3500;
        
        
        double amount;
        
        
        System.out.println ("1.Dept");
        System.out.println ("2.Withdrawal");
        System.out.println ("3.Your Balance");
        System.out.println ("4.Exit");
        int num = scan.nextInt();
        
        if(num == 1)
       {
        System.out.println ("Deposit Amount?");
        amount = scan.nextDouble();
      
        
        startingBalance = startingBalance + amount;
        System.out.println("Balance IS");
        System.out.println(balance);
      
        }
        else if (num == 2)
        {
      
        System.out.println("Witdraw Amount?");
        amount = scan.nextDouble();
      
        
        startingBalance = startingBalance - amount;
        System.out.println("Balance is");
        System.out.println(balance);
        
        }
        else if (num == 3)
        {
        
    
        System.out.println("Your Balance");
        System.out.println(balance);
        
        }
        else if (num == 4)
        {
        

     } while (num != 4)
        
        System.out.println ("Good Bye!");
            
    }

}
}
User is offlineProfile CardPM

Go to the top of the page

markhazlett9
post 11 Oct, 2008 - 08:48 AM
Post #6


D.I.C Head

**
Joined: 12 Jul, 2008
Posts: 132



Thanked 8 times
My Contributions


Hey there, you were close

Remember a couple things... 2. the end of a do-while loop must be included after the set of braces that closes the do loop not before. Also try to initialize most of your variables at the top of your code unless they are going to be specifically initialized for a loop. Also the else-if statement that is for num == 4, you will need to include code in it to exit the program... something like system.exit(Variable) will do the trick just fine. Also when you are getting input from the user try not to initialize the variable on input and do it all above if you can... here is what it might look like....
CODE
import java.util.Scanner;

public class Bank11 {

    public static void main(String[] args) {

        
        Scanner scan = new Scanner(System.in);
        int num;
      
        do {    

        double balance = 3500;
        double startingBalance = 0;
        
        
        double amount;
        
        
        System.out.println ("1.Dept");
        System.out.println ("2.Withdrawal");
        System.out.println ("3.Your Balance");
        System.out.println ("4.Exit");
        num = scan.nextInt();
        
        if(num == 1)
       {
        System.out.println ("Deposit Amount?");
        amount = scan.nextDouble();
      
        
        startingBalance = startingBalance + amount;
        System.out.println("Balance IS");
        System.out.println(balance);
      
        }
        else if (num == 2)
        {
      
        System.out.println("Witdraw Amount?");
        amount = scan.nextDouble();
      
        
        startingBalance -= amount;
        System.out.println("Balance is");
        System.out.println(balance);
        
        }
        else if (num == 3)
        {
        
    
        System.out.println("Your Balance");
        System.out.println(balance);
        
        }
        
            
    }while (num != 4);
        
        System.out.println ("Good Bye!");

}
}
User is offlineProfile CardPM

Go to the top of the page

HangTight
post 11 Oct, 2008 - 10:43 AM
Post #7


New D.I.C Head

*
Joined: 26 Sep, 2008
Posts: 14


My Contributions


WOW, thanks for the help it works PERFECT!!!! I should have known to declare "int num;" The Help was icon_up.gif icon_up.gif icon_up.gif icon_up.gif
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/21/08 08:57AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month