I'm a newb in Java.
how do I make the loop stop when the user enters 0.
Is the codes right so far?
I can't seem to run it, theres 1 error saying (no source location)


The output is suppose to look like this:
1. show total in bank.
2. Add a penny.
3. add a nickel
4. add a dime.
5. add a quarter
6. take money out of bank.
Enter 0 to quit.
Enter your choice: 5

import java.util.Scanner;
 
 
public class MySavings {
 
 
    public static void main(String[] args) {
    
        Scanner input = new Scanner(System.in);
        int total = 0;
 
        System.out.println("1. Show total in bank: ");
        System.out.println("2. Add a penny");
        System.out.println("3. Add a nickel");
        System.out.println("4. Add a dime");
        System.out.println("5. Add a quarter");
        System.out.println("6. Take money out of bank");
        System.out.print("Enter 0 to quit"); 
        System.out.print("Enter your choice: ");
        int choice = input.nextInt();
 
        if (choice == 1)
        {
            System.out.println(total);
        }
        else if(choice == 2)
        {
            total = total + 0.01; // <-- this is 1 penny
            System.out.println(total);
        }
        else if(choice == 3)
        { 
            total = total + 0.05; // <-- this is nickel  
            System.out.println(total); 
        } 
        else if(choice == 4);
        {
            total = total + 0.10; // <-- this is dime
            System.out.println(total); 
        } 
        else if(choice == 5);
        {
            total = total + 0.25; // <-- this is quarter
            System.out.println(total); 
        } 
        else if(choice == 6); 
            System.out.print("You have" + total ); 
        {
        else if(choice == 0); 
            
        else 
            System.out.println("You choose ");

Try wrapping your if and else if statements in a while loop.

while(choice != 0)
{
if...
else if...
else...
}

This should stop processing when the user enters 0.

Okay thanks.
Would I need to indent all the codes below after I do the while loop? or just keep it the same?

Indentation is not an issue in Java. Just keep all your previous code inside the braces {}.
By the way, you can delete the line

else if(choice == 0);

The while loop replaces this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.