简体   繁体   中英

Unable to call Multiple methods in Main method of Java Calculator program

This is a simple Calculator java program. I am trying to build 4 operations (+, -, * & /) using java.

To make program modular I have created multiple method and calling them in Main() method. Problem is method 'calcContinue();' is not invoking from the main method. I am not getting error or warnings. On running, main() method is invoking chooseOperation() and allow user to enter num1 and num2. However, nothing is after that. I am expecting main() to invoke 'calcContinue() method as well.

Please advise suggestions how can I correct my code to make 'calcContinue()' called from the main.

import java.util.*;

public class Calculator {

    static double num1, num2;
    static int operator;
    static double result;
    static Scanner scanObj = new Scanner(System.in);

    public static void main(String[] args) {        //main method
        System.out.print("enter first number:");                                            //user will enter number 1
        num1 = scanObj.nextDouble();

        System.out.print("enter Second number:");                                           //user will enter number 1
        num2 = scanObj.nextDouble();

        chooseOperation();
        calcContinue();
    }

    public static void chooseOperation() {          //user will choose calculator operation using 1-4

        System.out.println("Please select"
                + "\n 1. Addition "
                + "\n 2. Subtraction"
                + "\n 3. Multiplication"
                + "\n 4. Division");

        System.out.print("enter the calculator operation\t" + scanObj.nextLine());  //user will enter calculator operation
        operator = scanObj.nextInt();

        if (operator == 1 ) {
            addition();
        }

        else if (operator == 2) {
            subtraction();
        }

        else if (operator == 3) {
            multiplication();
        }

        else {
            division();
        }

    }

    public static char calcContinue() {                 //method for user to select if they want to continue
        System.out.print("do you want to perform more operation \t Y/N ?" + scanObj.next().charAt(0));

        char a = scanObj.next().charAt(0);

        if (a == 'Y' || a == 'y') {
            Calculator.chooseOperation();

        } else {
            System.out.println("Calculator operation is terminated");
        }

        return a;
    }

    public static void addition() {     //method for addition
        result = num1 + num2;
        System.out.println("Addition of\t" +num1+ "and" +num2+ "is\t:" + result);
    }

    //method for subtraction
    public static void subtraction() {
        result = num1 - num2;
        System.out.println("subtraction of\t" +num1+ "and" +num2+ "is:\t" + result);
    }

    //method for multiplication
    public static void multiplication() {
        result = num1 * num2;
        System.out.println("multiplication of\t" +num1+ "and" +num2+ "is:\t" + result);
    }

    //method for division
    public static void division() {
        result = num1/num2;
        System.out.println("Division of\t" +num1+ "and" +num2+ "is:\t" + "" + result);
    }
}

First of all, your code works just fine. The problem is that right at the beginning of calcContinue() you tell java IO to wait 2 times to enter an input. Just replace

System.out.print("do you want to perform more operation \t Y/N?" + scanObj.next().charAt(0));

with

System.out.print("do you want to perform more operation \t Y/N?");

In the next line you are reading the input from the user. So there is no need for calling scanObj.next().charAt(0) inside the print statement.

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