-1

I am not sure if I am using public static int correctly as well. All in all I feel like this code is a giant mess where I just kept adding more and more to it that I probably don't need, but this point I still cant quite figure out how to get it to loop the way I want it to.

import java.util.Scanner;

public class Program7 {

    public static void main(String[] args) {
        // main menu
        mm();

        int choice = 0;
        switch (choice) {
            // addition
            case 1:
                add();  
                break;

            // subtraction
            case 2:
                sub();
                break;

            // exit
            case 3:
                System.out.print("GOOD BYE ^__^");  
                break;

            // if choice >3 or <1
            default:
                System.out.print("\nInvalid selection.\nPlease select from (1-3): ");
                Scanner kb = new Scanner(System.in);
                choice = kb.nextInt();
        }
   }

addition

public static int add() {
    System.out.print("\nEnter number1: ");
    Scanner kb = new Scanner(System.in);
    int num1 = kb.nextInt();

    System.out.print("Enter number2: ");
    int num2 = kb.nextInt();
    int sum = num1 + num2;

    System.out.print("\n" + num1 + " + " + num2 + " = " + sum);
    return sum; 
}

subtraction

public static int sub() {
    System.out.print("Enter number1: ");
    Scanner kb = new Scanner(System.in);
    int num1 = kb.nextInt();

    System.out.print("Enter number2: ");
    int num2 = kb.nextInt();
    int diff = num1 - num2;

    System.out.print(num1 + " - " + num2 + " = " + diff);
    return diff;
}

main menu for invalid number (<1 or >3) and/or after user finishes their choice until they select choice number 3:

public static int mm() {
    System.out.print("==MAIN MENU==\n1.Addition\n2.Subtraction\n3.Exit\n\nSelect Menu(1-3): "); 
    Scanner kb = new Scanner(System.in);
    int choice = kb.nextInt();
    return choice;
}
agabrys
  • 8,728
  • 3
  • 35
  • 73

2 Answers2

1

The code is mostly done. To put it in a loop, use while(true):

public static void main(String[] args) {
    while (true) {
        int choice = mm();
        // rest of method

When exiting, instead of using break; which exits the switch block, use System.exit(0); which completely exits the program.

Ypnypn
  • 933
  • 1
  • 8
  • 21
0

Something like this should suffice:

while( true )
{
    // Display menu and let user pick one choice.
    switch( choice )
    {
        // Other cases till here.
        default: exit.
    }
}
Pavan Dittakavi
  • 3,013
  • 5
  • 27
  • 47