Home > Software design >  How to add the option for the user to repeat this Java program AFTER using it?
How to add the option for the user to repeat this Java program AFTER using it?

Time:03-23

I've been at this program all night and I've just got one thing I need to add, but I'm scratching my head on how to do it. How would I go about adding the option to repeat the program after the user has already gone through it once? I've got the option in for the user to enter whether they would like to use it in the first place, with Y running the program, then N ending it. I just need some help with getting the program to go back to the very beginning if the user has already ran through it, or quit again if they would like to just exit. Thank you! And I'm sorry that my code isn't the prettiest. I'm very much a beginner.

import java.util.Scanner;

public class BarChart 

{
    public static void main(String[] args)

    {
        int store1, store2, store3, store4, store5;
        String answer;

        Scanner keyboard = new Scanner(System.in);

        System.out.println("This program will display a bar chart "   
        " comprised of astericks based on five different stores' "  
        "sales today. 1 asterick = $100 in sales.");

        System.out.println("Would you like to use the program? "
          "type 'Y' for yes or 'N' to exit the program.");
        answer = keyboard.nextLine();

        if(answer.equalsIgnoreCase("Y"))
        {
            System.out.print("Enter today's sales for store 1: ");
            store1 = keyboard.nextInt();
            while(store1 < 0)
            {
                System.out.println("Error! Sales cannot be less than 0.");
                System.out.print("Enter today's sales for store 1: ");
                store1 = keyboard.nextInt();
            }

            System.out.print("Enter today's sales for store 2: ");
            store2 = keyboard.nextInt();
            while(store2 < 0)
            {
                System.out.println("Error! Sales cannot be less than 0.");
                System.out.print("Enter today's sales for store 2: ");
                store2 = keyboard.nextInt();
            }

            System.out.print("Enter today's sales for store 3: ");
            store3 = keyboard.nextInt();
            while(store3 < 0)
            {
                System.out.println("Error! Sales cannot be less than 0.");
                System.out.print("Enter today's sales for store 3: ");
                store3 = keyboard.nextInt();
            }

            System.out.print("Enter today's sales for store 4: ");
            store4 = keyboard.nextInt();
            while(store4 < 0)
            {
                System.out.println("Error! Sales cannot be less than 0.");
                System.out.print("Enter today's sales for store 4: ");
                store4 = keyboard.nextInt();
            }

            System.out.print("Enter today's sales for store 5: ");
            store5 = keyboard.nextInt();
            while(store5 < 0)
            {
                System.out.println("Error! Sales cannot be less than 0.");
                System.out.print("Enter today's sales for store 5: ");
                store5 = keyboard.nextInt();
            }

            System.out.println("Sales \t Bar Chart");
            System.out.println("----- \t --------------------------");

            System.out.print("Store 1: ");        
            for (int num = 0; num < store1; num  = 100)
            {
                System.out.print("*");
            }

            System.out.print("\nStore 2: ");
            for (int num = 0; num < store2; num  = 100)
            {
                System.out.print("*");
            }

            System.out.print("\nStore 3: ");
            for (int num = 0; num < store3; num  = 100)
            {
                System.out.print("*");
            }

            System.out.print("\nStore 4: ");
            for (int num = 0; num < store4; num  = 100)
            {
                System.out.print("*");
            }

            System.out.print("\nStore 5: ");
            for (int num = 0; num < store5; num  = 100)
            {
                System.out.print("*");
            }
        }
        
        if(answer.equalsIgnoreCase("N"))
        System.out.print("Program terminated. Have a nice day!");

    }
    
}

I've tried reorienting the loops, but that just breaks my program.

CodePudding user response:

You currently have:

System.out.println("Would you like to use the program? "
      "type 'Y' for yes or 'N' to exit the program.");
    answer = keyboard.nextLine();

    if(answer.equalsIgnoreCase("Y")) {

// "the program"

    }
    if(answer.equalsIgnoreCase("N"))
    System.out.print("Program terminated. Have a nice day!");

Change it to:

while (true) {

    // "the program"

    System.out.println("Would you like to use the program again? "
      "type 'Y' for yes or anything else to exit the program.");
    answer = keyboard.nextLine();

    if (!answer.equalsIgnoreCase("Y")) {
        System.out.print("Program terminated. Have a nice day!");
        break;
    }
}

while(true) loops forever until break is executed.

CodePudding user response:

you should use a loop to iterate over the code while the anwser of starting the program is YES. if the anwser is NO we should break out of the loop. this is a working code:

 public static void main(String[] args) {
    int store1, store2, store3, store4, store5;
    String answer;

    Scanner keyboard = new Scanner(System.in);

    System.out.println("This program will display a bar chart "  
            " comprised of astericks based on five different stores' "  
            "sales today. 1 asterick = $100 in sales.");

    System.out.println("Would you like to use the program? "
              "type 'Y' for yes or 'N' to exit the program.");
    answer = keyboard.nextLine();

    while (answer.equalsIgnoreCase("Y")) {
        System.out.print("Enter today's sales for store 1: ");
        store1 = keyboard.nextInt();
        while (store1 < 0) {
            System.out.println("Error! Sales cannot be less than 0.");
            System.out.print("Enter today's sales for store 1: ");
            store1 = keyboard.nextInt();
        }

        System.out.print("Enter today's sales for store 2: ");
        store2 = keyboard.nextInt();
        while (store2 < 0) {
            System.out.println("Error! Sales cannot be less than 0.");
            System.out.print("Enter today's sales for store 2: ");
            store2 = keyboard.nextInt();
        }

        System.out.print("Enter today's sales for store 3: ");
        store3 = keyboard.nextInt();
        while (store3 < 0) {
            System.out.println("Error! Sales cannot be less than 0.");
            System.out.print("Enter today's sales for store 3: ");
            store3 = keyboard.nextInt();
        }

        System.out.print("Enter today's sales for store 4: ");
        store4 = keyboard.nextInt();
        while (store4 < 0) {
            System.out.println("Error! Sales cannot be less than 0.");
            System.out.print("Enter today's sales for store 4: ");
            store4 = keyboard.nextInt();
        }

        System.out.print("Enter today's sales for store 5: ");
        store5 = keyboard.nextInt();
        while (store5 < 0) {
            System.out.println("Error! Sales cannot be less than 0.");
            System.out.print("Enter today's sales for store 5: ");
            store5 = keyboard.nextInt();
        }

        System.out.println("Sales \t Bar Chart");
        System.out.println("----- \t --------------------------");

        System.out.print("Store 1: ");
        for (int num = 0; num < store1; num  = 100) {
            System.out.print("*");
        }

        System.out.print("\nStore 2: ");
        for (int num = 0; num < store2; num  = 100) {
            System.out.print("*");
        }

        System.out.print("\nStore 3: ");
        for (int num = 0; num < store3; num  = 100) {
            System.out.print("*");
        }

        System.out.print("\nStore 4: ");
        for (int num = 0; num < store4; num  = 100) {
            System.out.print("*");
        }

        System.out.print("\nStore 5: ");
        for (int num = 0; num < store5; num  = 100) {
            System.out.print("*");
        }

        keyboard.nextLine();
        System.out.println("");
        System.out.println("Would you like to reuse the program? "
                  "type 'Y' for yes or 'N' to exit the program.");
        answer = keyboard.nextLine();
    }


    if (answer.equalsIgnoreCase("N"))
        System.out.print("Program terminated. Have a nice day!");

}
  • Related