Getting my Return choice to work inside of the calculator

75 Views Asked by At
import java.util.Scanner;

public class brickgame {

    public brickgame() {
    }
   public static void main (String[] args) {
    System.out.println("Choose the operation you want to do?");
    Scanner charles = new Scanner(System.in);
    String number;
    
    
   
    System.out.print("[1] Biography \n[2] Calculator \n[3] Bye \nChoose the number:");
        number=charles.nextLine();
        
        
        if(number.equals("1")) {
            Scanner naria = new Scanner(System.in);
        String Fname,Age,Address,Birthplace,Birthday,FavoriteF,FavoriteS,FavoriteC,Hobby,STalent;
        
        System.out.println("Please use Underscore Character or _ as your spacebar! ");
        
        System.out.print("Please Enter your Name: ");
        Fname = naria.next();
        
        System.out.print("Please Enter your Age: ");
        Age = naria.next();
        
        System.out.print("Please Enter your Address: ");
        Address = naria.next();
        
        System.out.print("Please Enter your Birthplace: ");
        Birthplace = naria.next();
        
        System.out.print("Please Enter your Birthday: ");
        Birthday = naria.next();
        
        System.out.print("Please Enter your Favorite Food: ");
        FavoriteF = naria.next();
        
        System.out.print("Please Enter your Favorite Subject: ");
        FavoriteS = naria.next();
        
        System.out.print("Please Enter your Favorite Color: ");
        FavoriteC = naria.next();
        
        System.out.print("Please Enter your Hobby: ");
        Hobby = naria.next();
        
        System.out.print("Please Enter your Secret Talent: ");
        STalent = naria.next();
        
        System.out.println("Hello "+Fname);
        System.out.println("You are "+ Age +" years old");
        System.out.println("You lived at "+Address);
        System.out.println("You are born in "+ Birthplace);
        System.out.println("Your birthday is on "+Birthday);
        System.out.println("Your Favorite Food is "+FavoriteF);
        System.out.println("Your Favorite Color is "+FavoriteC);
        System.out.println("Your Favorite Color is "+FavoriteS);
        System.out.println("Your hobby is to "+Hobby);
        System.out.println("Your Secret Talent is to "+ STalent);
        }
    else if(number.equals("2")) {
        while (true)
        {
        
        Scanner glenn = new Scanner(System.in);
String chosen;
int a,b,ab,e,f;
float d,c,dc;


System.out.println("\n[1] Addition \n[2] Subtraction \n[3] Multiplication \n[4] Division \n[5] Greater Than/Less Than \n[6] Return \nEnter the number you chose:");
    chosen=glenn.nextLine();
        
            if(chosen.equals("1")) {
            System.out.print("Enter your first number: ");
            a=glenn.nextInt();
            System.out.print("Enter your second number: ");
            b=glenn.nextInt();
            ab=a+b;         
            System.out.print(a + " + " + b + " = " +ab);     
            }
        else if(chosen.equals("2")) {
            System.out.print("Enter your first number: ");
            a=glenn.nextInt();
            System.out.print("Enter your second number: ");
            b=glenn.nextInt();
            ab=a-b;
                
            System.out.print(a + " - " + b + " = " +ab);
            }
        else if(chosen.equals("3")) {
            System.out.print("Enter your first number: ");
            a=glenn.nextInt();
            System.out.print("Enter your second number: ");
            b=glenn.nextInt();
            ab=a*b; 
            System.out.print(a + " * " + b + " = " +ab);
            }
    else if(chosen.equals("4")) {
        System.out.print("Enter your first number: ");
        c=glenn.nextInt();
        System.out.print("Enter your second number: ");
        d=glenn.nextInt();
        dc=c/d; 
        System.out.print(c + " / " + d + " = " +dc);
    }
        else if(chosen.equals("5")) {
        System.out.print("Enter your first number: ");
        e=glenn.nextInt();
        System.out.print("Enter your second number: ");
        f=glenn.nextInt();
        if (e>f){
            System.out.print(e + " is greater than "+ f);
         }
         
          if(e<f){
            System.out.print(e + " is lesser than "+ f);
         }
         if(e==f){
            System.out.print(e + " is same as "+ f);
         }
        }
          if(chosen.equals("6")) {
            System.out.println(" \nThank you and Goodbye!");
        break;
    }   
        }
    }
        else System.out.println(" \nThank you and Goodbye!"); 

}
}

I wanted the break to change to return back to biography and calculator choice, I need help please because Ive been struggling too much now, I dont know what statements do I use to make it work, I only got like a few hours to finish it and Ive been struggling real, real hard for the past 2 hours

It is an easy problem but I am only a programmer for 2 weeks rn thats why I needed help, any help is appreaciated

2

There are 2 best solutions below

2
Dolfinwu On

If you want to implement a way to return to the menu after completing an operation, this is what I would do:

  1. Define a boolean variable to control the menu loop. You can use true to keep the menu running until the user wants to exit.
boolean isRunning = true;
  1. Then put the entire menu system in a while loop to keep the menu active.
while (isRunning) {
    // Menu code
}
  1. When the user selects an option (e.g., "1" for Biography or "2" for Calculator), you can add an additional loop to handle the specific operation and return to the main menu

For biography:

if (number.equals("1")) {
    // Biography code

    // After the executino above, add a loop to ask if the user wants to return to the main menu
    System.out.print("Return to the main menu? (yes/no): ");
    String returnChoice = naria.next();
    
    if (returnChoice.equalsIgnoreCase("no")) {
        isRunning = false; // If not, then exit the program
    }
}

For Calculator:

else if (number.equals("2")) {
    while (true) {
        // Calculator code
        
        // After performing the code above, ask if the user wants to return to the main menu
        System.out.print("Return to the main menu? (yes/no): ");
        String returnChoice = glenn.next();
        
        if (returnChoice.equalsIgnoreCase("yes")) {
            break; // Then exit this loop and return to the main menu
        }
    }
}
0
Reilas On

"... I wanted the break to change to return back to biography and calculator choice ... I dont know what statements do I use to make it work ..."

Use a loop.

A lot of the "bugs" in your code stem from the use of the Scanner#next methods versus nextLine.

The next methods are useful for capturing multiple values within a single line, and not the entire line itself.

Here is a re-factor, where I'm using a LinkedHashMap to contain the questions and answers for the biography option.

public brickgame() {
}
public static void main (String[] args) {
    Scanner in = new Scanner(System.in);
    Map<String, String[]> map = new LinkedHashMap<>() {{
        put("Fname", new String[] { "Please Enter your Name: ", null });
        put("Age", new String[] { "Please Enter your Age: ", null });
        put("Address", new String[] { "Please Enter your Address: ", null });
        put("Birthplace", new String[] { "Please Enter your Birthplace: ", null });
        put("Birthday", new String[] { "Please Enter your Birthday: ", null });
        put("FavoriteF", new String[] { "Please Enter your Favorite Food: ", null });
        put("FavoriteS", new String[] { "Please Enter your Favorite Subject: ", null });
        put("FavoriteC", new String[] { "Please Enter your Favorite Color: ", null });
        put("Hobby", new String[] { "Please Enter your Hobby: ", null });
        put("STalent", new String[] { "Please Enter your Secret Talent: ", null });
    }};

    Formatter f = new Formatter(System.out);
    boolean exit = false;

    do {
        f.format("Choose the operation you want to do?%n");
        f.format("[1] Biography%n");
        f.format("[2] Calculator%n");
        f.format("[3] Bye%n");
        f.format("Choose the number:").flush();
        switch (Integer.parseInt(in.nextLine())) {
            case 1 -> {
                for (Map.Entry<String, String[]> e : map.entrySet()) {
                    f.format(e.getValue()[0]).flush();
                    e.getValue()[1] = in.nextLine();
                }

                f.format("Hello "+map.get("Fname")[1] + "%n");
                f.format("You are "+ map.get("Age")[1] +" years old%n");
                f.format("You lived at "+map.get("Address")[1] + "%n");
                f.format("You are born in "+ map.get("Birthplace")[1] + "%n");
                f.format("Your birthday is on "+map.get("Birthday")[1] + "%n");
                f.format("Your Favorite Food is "+map.get("FavoriteF")[1] + "%n");
                f.format("Your Favorite Color is "+map.get("FavoriteC")[1] + "%n");
                f.format("Your Favorite Color is "+map.get("FavoriteS")[1] + "%n");
                f.format("Your hobby is to "+map.get("Hobby")[1] + "%n");
                f.format("Your Secret Talent is to "+ map.get("STalent")[1] + "%n");
                f.flush();
            }
            case 2 -> {
                int i;
                BigDecimal x, a, b;
                f.format("[1] Addition%n");
                f.format("[2] Subtraction%n");
                f.format("[3] Multiplication%n");
                f.format("[4] Division%n");
                f.format("[5] Greater Than/Less Than%n");
                f.format("[6] Return%n");
                f.format("Enter the number you chose:").flush();
                i = Integer.parseInt(in.nextLine());
                f.format("Enter your first number: ").flush();
                a = new BigDecimal(in.nextLine());
                f.format("Enter your second number: ").flush();
                b = new BigDecimal(in.nextLine());
                switch (i) {
                    case 5 -> {
                        f.format(a.toPlainString() + " is ");
                        f.format(
                            switch (a.compareTo(b)) {
                                case -1 -> "lesser-than ";
                                case 0 -> "equal-to ";
                                default -> "greater-than ";
                            });
                        f.format(b.toPlainString()).flush();
                    }
                    case 6 -> {
                        System.out.println(" \nThank you and Goodbye!");
                        exit = true;
                    }
                    default -> {
                        x = new BigDecimal(
                                String.valueOf(
                                    switch (i) {
                                        case 1 -> a.add(b);
                                        case 2 -> a.subtract(b);
                                        case 3 -> a.multiply(b);
                                        default -> a.divide(b, 0xf, RoundingMode.HALF_UP);
                                    }));
                        String op = switch (i) {
                            case 1 -> "+";
                            case 2 -> "-";
                            case 3 -> "*";
                            default -> "/";
                        };
                        f.format(a.toPlainString());
                        f.format(" %s ", op);
                        f.format(b.toPlainString());
                        f.format(" = %s%n", x.toPlainString());
                    }
                }
            }
            case 3 -> {
                System.out.println(" \nThank you and Goodbye!");
                exit = true;
            }
        }
    } while (!exit);
}

And, here is an example usage.

Choose the operation you want to do?
[1] Biography
[2] Calculator
[3] Bye
Choose the number:1
Please Enter your Name: abc
Please Enter your Age: 123
Please Enter your Address: 456 def
Please Enter your Birthplace: ghi
Please Enter your Birthday: 7/8/9
Please Enter your Favorite Food: jkl
Please Enter your Favorite Subject: mno
Please Enter your Favorite Color: pqr
Please Enter your Hobby: stu
Please Enter your Secret Talent: vwx
Hello abc
You are 123 years old
You lived at 456 def
You are born in ghi
Your birthday is on 7/8/9
Your Favorite Food is jkl
Your Favorite Color is pqr
Your Favorite Color is mno
Your hobby is to stu
Your Secret Talent is to vwx
Choose the operation you want to do?
[1] Biography
[2] Calculator
[3] Bye
Choose the number:2
[1] Addition
[2] Subtraction
[3] Multiplication
[4] Division
[5] Greater Than/Less Than
[6] Return
Enter the number you chose:4
Enter your first number: 22
Enter your second number: 7
22 / 7 = 3.142857142857143
Choose the operation you want to do?
[1] Biography
[2] Calculator
[3] Bye
Choose the number:3