1

I'm new to java and this forum. I wrote a code for a simple calculator. It's working. But how can I repeat the main method if I (let's say) put "=" instead of "(+, -, *, /)"? Should I use a loop, or something else? Thanks in advance!

import java.util.Scanner;

public class SimCal {

    public static int add(int a, int b) {
        return a + b;
    }

    public static int sub(int a, int b) {
        return a - b;
    }

    public static int mul(int a, int b) {
        return a * b;
    }

    public static int div(int a, int b) {
        return a / b;
    }

    public static void main(String[] args) {

        Scanner scan1 = new Scanner(System.in);
        System.out.println("What do you want to do (+, -, *, /)? ");
        String input1 = scan1.nextLine();

        if (!input1.equals("+") && !input1.equals("-") && !input1.equals("*") && !input1.equals("/")) { // if wrong input given
            System.out.println("You must Enter a valid operator");
        } else {
            Scanner scan2 = new Scanner(System.in);
            System.out.println("Enter first number: ");
            int input2 = scan2.nextInt();

            Scanner scan3 = new Scanner(System.in);
            System.out.println("Enter second number: ");
            int input3 = scan3.nextInt();

            if (input1.equals("+")) {
                System.out.println(add(input2, input3));
            } else if (input1.equals("/")) {
                System.out.println(div(input2, input3));
            } else if (input1.equals("-")) {
                System.out.println(sub(input2, input3));
            } else {
                System.out.println(mul(input2, input3));
            }
            scan1.close();
            scan2.close();
            scan3.close();
        }
    }
}
ncw
  • 1,635
  • 1
  • 18
  • 26
Un_lh
  • 9
  • 2

1 Answers1

0

I am a bit unsure of what you are asking, but I understood it that you want to be able to repeat the calculator without having to run it again. This can be achieved by using a boolean and a while block.

Here is an example:

import java.util.Scanner;

public class SimCal {

    public static int add (int a, int b){
        return a+b;
    }
    public static int sub (int a, int b){
        return a-b;
    }
    public static int mul (int a, int b){
        return a*b;
    }
    public static int div (int a, int b){
        return a/b;
    }
    public static boolean done = false;


    public static void main(String[] args){


        Scanner scan1 = new Scanner(System.in);
        Scanner scan2 = new Scanner(System.in);
        Scanner scan3 = new Scanner(System.in);
        while (!done) {
            System.out.println("What do you want to do (+, -, *, /, quit)? ");
            String input1 = scan1.nextLine();

            if (!input1.equals("+") && !input1.equals("-") && !input1.equals("*") && !input1.equals("/") && !input1.equals("quit")) 
            { //if wrong input given
                System.out.println("You must Enter a valid operator");
            } 
            else if (input1.equals("quit")) 
            {
                done = true;
                scan1.close();
                scan2.close();
                scan3.close();
            } 
            else 
            {
                System.out.println("Enter first number: ");
                int input2 = scan2.nextInt();


                System.out.println("Enter second number: ");
                int input3 = scan3.nextInt();

                if (input1.equals("+")) 
                {
                    System.out.println(add(input2, input3));
                } 
                else if (input1.equals("/")) 
                {
                    System.out.println(div(input2, input3));
                } 
                else if (input1.equals("-")) 
                {
                    System.out.println(sub(input2, input3));
                } 
                else 
                {
                    System.out.println(mul(input2, input3));

                }

            }
        }
    }
}

I hope this is helpful. Like Andy Turner mentioned, you should try to not use multiple scanners.

EDIT: I forgot to close 2 scanners. Also, switch cases can be a better way of doing this, like mentioned by Saurav Sahu.

Roxcly
  • 146
  • 3
  • 18