Home > Software engineering >  How to make a loop for this script?
How to make a loop for this script?

Time:07-17

I am working on a simple/scientific calculator in java, and I am having trouble putting this in a while loop so the user can continuously use the calculator. I've tried putting it in different places in the code, but it either repeats the input section or doesn't repeat anything. Any tips? Here is my code below:

static Scanner s1 = new Scanner(System.in);
public static void main(String[] args) {

    
    System.out.println("Welcome to my calculator:");
    String operator = "";
    Scanner op = new Scanner(System.in);
    System.out.println("Type 1 if you wish to use the Standard calculator, 2 for the Scientific calculator, or QUIT if you wish to quit the program.");
    operator = op.nextLine();
    if (operator.equals("1")) {
    System.out.println(standard());
    }
    if (operator.equals("2")) {
    System.out.println(scientific());
    }
    if (operator.equals("QUIT")) {
    System.out.print("System quit");
    
    }
    }
    public static int standard() {
    //The system will print 0 at the end to show that it's working
        System.out.println("Standard Calculator chosen.");
        System.out.println("Type 1 if you wish to use addition, 2 for subtraction, 3 for multiplication, 4 for exponent, 5 for division, or 6 for mod.");
        int input2 = s1.nextInt();
        int num1 = 0;
        int num2 = 0;
    //String loop = "";
        switch (input2) {
        case 1:
            System.out.println("(Add chosen) Please enter the first value: ");
            num1 = s1.nextInt();
            System.out.println("Please enter the second value: ");
            num2 = s1.nextInt();
            System.out.println("Addition - ("   num1   " "   num2   ") = "   addExact(num1, num2));
            break;
        case 2:
            System.out.println("(Sub chosen) Please enter the first value: ");
            num1 = s1.nextInt();
            System.out.println("Please enter the second value: ");
            num2 = s1.nextInt();
            System.out.println("Subtration - ("   num1   "-"   num2   ") = "   subtractExact(num1, num2));
            break;
        case 3:
            System.out.println("(Multi chosen) Please enter the first value: ");
            num1 = s1.nextInt();
            System.out.println("Please enter the second value: ");
            num2 = s1.nextInt();
            System.out.println("Multiplication - ("   num1   "*"   num2   ") = "   multiplyExact(num1, num2));
            break;
        case 4:
            System.out.println("(Exp chosen) Please enter the first value: ");
            num1 = s1.nextInt();
            System.out.println("Please enter the exponent: ");
            num2 = s1.nextInt();
            System.out.println("Exponent - ("   num1   "^"   num2   ") = "   Math.pow(num1, num2));
            break;
        case 5:
            System.out.println("(Div chosen) Please enter the first value: ");
            num1 = s1.nextInt();
            System.out.println("Please enter the second value: ");
            num2 = s1.nextInt();
            System.out.println("Division - ("   num1   "/"   num2   ") = "   floorDiv(num1, num2));
            break;
        case 6:
            System.out.println("(Mod chosen) Please enter the first value: ");
            num1 = s1.nextInt();
            System.out.println("Please enter the second value: ");
            num2 = s1.nextInt();
            System.out.println("Mod - ("   num1   "%"   num2   ") = "   floorMod(num1, num2));
            break;
    }
    return (0);
    }
    public static double scientific() {
    //The system will print 0.0 at the end to show that it's working
        System.out.println("Scientific Calculator chosen.");
        System.out.println("Type 1 for sin, 2 for cos, 3 for tan, 4 for floor, 5 for ceil, 6 for square root, 7 for cube root, 8 for rounding, 9 for min, 10 for max.");
        int input2 = s1.nextInt();
        double val1 = 0.0;
        double val2 = 0.0;
        switch (input2) {
        case 1:
            System.out.println("(Sin chosen) Please enter the value :");
            val1 = s1.nextDouble();
            System.out.println("Sin - ("   val1   ") = "   sin(val1));
            break;
        case 2:
            System.out.println("(Cos chosen) Please enter the value :");
            val1 = s1.nextDouble();
            System.out.println("Cos - ("   val1   ") = "   cos(val1));
            break;
        case 3:
            System.out.println("(Tan chosen) Please enter the value :");
            val1 = s1.nextDouble();
            System.out.println("Tan - ("   val1   ") = "   tan(val1));
            break;
        case 4:
            System.out.println("(Floor chosen) Please enter the value :");
            val1 = s1.nextDouble();
            System.out.println("Floor - ("   val1   ") = "   Math.floor(val1));
            break;
        case 5:
            System.out.println("(Ceil chosen) Please enter the value :");
            val1 = s1.nextDouble();
            System.out.println("Ceil - ("   val1   ") = "   Math.ceil(val1));
            break;
        case 6:
            System.out.println("(Square root chosen) Please enter the value :");
            val1 = s1.nextDouble();
            System.out.println("Square root - ("   val1   ") = "   sqrt(val1));
            break;
        case 7:
            System.out.println("(Cube root chosen) Please enter the value :");
            val1 = s1.nextDouble();
            System.out.println("Cube root - ("   val1   ") = "   cbrt(val1));
            break;
        case 8:
            System.out.println("(Round chosen) Please enter the value :");
            val1 = s1.nextDouble();
            System.out.println("Round - ("   val1   ") = "   round(val1));
            break;
        case 9:
            System.out.println("(Min chosen) Please enter the 1st value :");
            val1 = s1.nextDouble();
            System.out.println("Enter the 2nd value: ");
            val2 = s1.nextDouble();
            System.out.println("Minimum - ("   val1   ","   val2   ") = "   min(val1,val2));
            break;
        case 10:
            System.out.println("(Max chosen) Please enter the 1st value :");
            val1 = s1.nextDouble();
            System.out.println("Enter the 2nd value: ");
            val2 = s1.nextDouble();
            System.out.println("Maximum - ("   val1   ","   val2   ") = "   max(val1,val2));
            break;
    }
        return val2;
    }
    }
    }

CodePudding user response:

You need to do something as follows, as you want to repeat all the process until the user choose to QUIT the calculator app:

public static void main(String[] args) {
        System.out.println("Welcome to my calculator:");
        String operator = "";
        
        while (!operator.equals("QUIT")) {

            Scanner op = new Scanner(System.in);
            System.out.println("Type 1 if you wish to use the Standard calculator, 2 for the Scientific calculator, or QUIT if you wish to quit the program.");
            operator = op.nextLine();
            if (operator.equals("1")) {
                System.out.println(standard());
            }
            if (operator.equals("2")) {
                System.out.println(scientific());
            }
            if (operator.equals("QUIT")) {
                System.out.print("System quit");

            }

        }
    }

output:

Welcome to my calculator:
Type 1 if you wish to use the Standard calculator, 2 for the Scientific calculator, or QUIT if you wish to quit the program.
1
Standard Calculator chosen.
Type 1 if you wish to use addition, 2 for subtraction, 3 for multiplication, 4 for exponent, 5 for division, or 6 for mod.
1
(Add chosen) Please enter the first value: 
1
Please enter the second value: 
2
Addition - (1 2) = 3
0
Type 1 if you wish to use the Standard calculator, 2 for the Scientific calculator, or QUIT if you wish to quit the program.

CodePudding user response:

Here's a solution for your "main" method that:

  • uses a "while" loop and a boolean value "keepGoing" to decide if it should loop again (or exit)
  • uses a "switch" statement to handle calling different functions based on input
  • if "QUIT" input, it sets "keepGoing = false" so that the "while" loop will exit
  • defines one Scanner, and names it clearly ("scanner")
  • passes that single Scanner object to the methods which need it (standard and scientific)

Two other changes worth making:

  • remove the global static Scanner s1 – don't use global variables, it will lead to hard-to-find problems in your code
  • edit those method signatures to accept a Scanner parameter:
    public static int standard(Scanner s1)`
    public static double scientific(Scanner s1)
    

Here's the code:

Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to my calculator:");

String prompt = "Type 1 if you wish to use the Standard calculator, "  
        "2 for the Scientific calculator, or "  
        "QUIT if you wish to quit the program.";

boolean keepGoing = true;
while (keepGoing) {
    System.out.println(prompt);
    switch (scanner.nextLine()) {
        case "1" -> System.out.println(standard(scanner));
        case "2" -> System.out.println(scientific(scanner));
        case "QUIT" -> {
            System.out.print("System quit");
            keepGoing = false; // this ejects from the while loop
        }
    }
}
  • Related