I ned a while loop statment that makes the calculations loop and ask again

im not too sure how you add a loop statement to this. I want it to be a while loop that makes it do that the calculations repeat after finishing. I've tried but it just keeps on giving me errors. ive tried doing While statements but just will not work im not sure how you set it up as my teacher did not explain very well

import com.godtsoft.diyjava.DIYWindow;

public class Calculator extends DIYWindow {

    public Calculator() {
        
        
            
        // getting number one
        double number1 = promptForDouble("Enter a number");
        
        //getting number2
        int number2 = promptForInt("Enter an integer");
        
        //getting what to do with operation
        print("What do you want to do with these numbers?\nAdd\tSubtract\tMultiply\tDivide");
        String operation = input();
        //declaring variable here so the same one can be used
        double answer = 0;
        switch(operation) {
        case "add":
            answer = number1 + number2;
            print(number1 + " + " + number2 + " = " + answer);
            break;
        case "subtract":
            answer = number1 - number2;
            print(number1 + " - " + number2 + " = " + answer);
            break;
        case "multiply":
            answer = number1 * number2;
            print(number1 + " * " + number2 + " = " + answer);
            break;
        case "divide":
            
            try {   
                answer = number1 / number2;     
            }
            catch(ArithmeticException e) {
                print("A number cannot be divided by 0.");  
            }
            print(number1 + " / " + number2 + " = " + answer);
            break;
        }
        
        
        double double1 = promptForDouble("Enter a number");
        double double2 = promptForDouble("Enter another number");
        double double3 = promptForDouble("Enter one last number");
        print("What do you want to do with these numbers?\nAdd\tSubtrack\tMultiply\tDivide");
        String operation2 = input();
        double answer2 = 0;
        switch(operation2) {
        case "add":
            answer2 = double1 + double2 + double3;
            print(double1 + " + " + double2 + " + " + double3 + " = " + answer2);
            break;
        case "subtract":
            answer2 = double1 - double2 - double3;
            print(double1 + " - " + double2 + " - " + double3 + " = " + answer2);
            break;
        case "multiply":
            answer2 = double1 * double2 * double3;
            print(double1 + " * " + double2 + " * " + double3 + " = " + answer2);
            break;
        case "divide":
        
            try {   
                answer2 = double1 / double2 / double3;      
            }
            catch(ArithmeticException e) {
                print("A number cannot be divided by 0.");  
            }
            print(double1 + " / " + double2 + " / " + double3 + " = " + answer2);
            break;
        }
        
        want it to loop after the code on top 
        
        }
    
        
    
    
    
    private double promptForDouble(String prompt) {
        double number1 = 0;
        print(prompt);
        String number = input();
        
        try {   
            number1 = Double.parseDouble(number);   
        }
        catch(NumberFormatException e){
            print("That is not a number. Please enter a number.");
            number1 = promptForDouble(prompt);  
        }
        return number1;
    }
    

    private int promptForInt(String prompt) {
        int number2 = 0;
        print(prompt);
        String number = input();
        
        try {
        
            number2 = Integer.parseInt(number); }
        catch(NumberFormatException e) {
            print("That is not an integer. Enter an integer.");
            number2 = promptForInt(prompt); }
        return number2;
    }
    
    
        
        
    
    public static void main(String[] args) {
        new Calculator();

    }

}

1 answer

  • answered 2022-05-07 05:45 HienNg

    Put this in your main method:

    while (true)
    {
       Thread.sleep(1000); // optional, make some deplay for more nature
       new Calculator();
    }
    

    Or you can wrap all body blocks of the constructor inside the above while statement.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum