简体   繁体   中英

How do I make my calculator restart after giving the answers

So I've tried to make a Java calculator on console, and it's works decent, but when I get the result, I want it to restart like a loop, but I can't figure it out.

import java.util.Scanner;

public class calculator {

    public static void main(String[] args0) {
        Scanner test = new Scanner(System.in);
        int x;
        int y;
        String c;
        try {
            System.out.println("Insert a number ");
            x = test.nextInt();
            System.out.println("insert a value e.g * / + -");
            c = test.next();
            System.out.println("Insert another number");
            y = test.nextInt();

            if (c.equals("*")) {
                System.out.println("the total is " + x * y);
            } else if (c.equals("+")) {
                System.out.println("the total is " + (x + y));
            } else if (c.equals("-")) {
                System.out.println("the total is " + (x - y));
            } else if (c.equals("/")) {
                System.out.println("the total is " + (x / y));
            } else {
                System.out.println("you are an idiot");
            }
        } catch (Exception e) {
            System.out.println("Please enter correct value.");
        }
    }
}

Use while + break . Below is the working version:

import java.util.Scanner;

public class Calculator {

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

        int x;
        int y;
        String c;

        while (true) {
            try {
                System.out.println("Insert a number ");
                x = test.nextInt();

                System.out.println("insert a value e.g * / + -");
                c = test.next();

                System.out.println("Insert another number");
                y = test.nextInt();

                if (c.equals("*")) {
                    System.out.println("the total is " + x * y);
                } else if (c.equals("+")) {
                    System.out.println("the total is " + (x + y));

                } else if (c.equals("-")) {
                    System.out.println("the total is " + (x - y));
                } else if (c.equals("/")) {
                    System.out.println("the total is " + (x / y));
                } else {
                    System.out.println("you are an idiot");
                }

            } catch (Exception e) {
                System.err.println("Please enter correct value.");
            }

            System.out.println("Do you wish to continue(y/n)?");
            if (test.next().equals("n")) {
                break;
            }
        }

    }
}

You can use a while loop:

public static void main(String[] args0) {
    Scanner test = new Scanner(System.in);
    while(true) {
    ....

}

When you want to stop the calculator, you need enter ctrl - c .

Another way, use a boolean flag and let user dicide whether to continue:

public static void main(String[] args0) {
    boolean flag = true;
    Scanner test = new Scanner(System.in);
    while(flag) {
        ....
        System.out.println("enter y for continue, n for exit");
        c = test.next();
        if (c.equals("n")) {
           flag = false;
        } 
    }
}

Insert while loop before try and put everything below in it.put while(true) and this will repeat infinitely. If you want to press some key to stop make if condition and put break in it.

Scanner sc=new Scanner(System.in);
while(true){
...
if(sc.next()=='q') break; //this is if you want to quit after pressing q
...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM