Home > Blockchain >  why does this code asks for input twice to get excuted?
why does this code asks for input twice to get excuted?

Time:01-04

This code is about a calculator that allows the user to choose whether he is going to perform binary or unary operations

the problem here is that when the user enters 2 to choose unary operations it requires entering 2 twice so that its sub-menu would execute.

but it works fine on entering 1 to choose the binary operations, so why does it require inputting 2 twice in the second case?


public class NewMain {

    static Scanner in = new Scanner(System.in);
    static double num1, num2, num0;
    static int X, Y, Z;

    public static void main(String[] args) {
        do {
            if (main1() == 1) {  // the recall of main menu and comparison
                switch (sub1()) {
                    case 1:
                        // the recall of binary method and comparison
                        // addition operation
                        ADDITION();
                        break;
                    case 2:
                        // subtraction operation
                        SUBTRACTATION();
                        break;
                    case 3:
                        // multiplication opreration
                        MULTIPLICATION();
                        break;
                    case 4:
                        //division operation
                        DIVISION();
                        break;
                    case 5:
                        //reminder operation
                        REMAINDER();
                        break;
                    default:
                    // donothing (infinite loop with -1 as exit value)
                }

            } // end of binary
            else if (main1() == 2) {
                switch (sub2()) {
                    case 1:
                        NUMBERINCREMENT();
                        break;
                    case 2:
                        NUMBERDECREMENT();
                        break;
                    case 3:
                        NUMBERSQUARED();
                        break;
                    case 4:
                        SQUARE_ROOT();
                        break;
                    default:
                    /// ininite looped that is why there is no break
                }
            } else {
                break;
            }
        } while (true);

    }

    public static int main1() {  // main menu
        System.out.println("<CALCULATOR> \n\n"
                  "Enter (1) To Use BINARY OPERATIONS \n"
                  "Enter (2) To Use UNARY OPERATIONS\n"
                  "Enter (-1) To Terminate The Program\n");

        X = in.nextInt(); // value of main1()

        return X;//////////////
    }

    public static int sub1() {  // binary submenu
        String line = "BINARY OPERATIONS Are Selected.  \n "
                  "Please Identify Which Operation You Are Going To Use By Entering It's Number. \n\n "
                  "1- ADDITION \n"
                  "2- SUBTRACTATION \n"
                  "3- MULTIPLICATION \n"
                  "4- DIVISION \n"
                  "5- REMAINDER \n";
        System.out.println(line);
        Y = in.nextInt();
        return Y;
    }

    public static void ADDITION() {
        System.out.println("ADDITION OPERATION Is Selected.\n"
                  "Enter The First Number: ");
        num1 = in.nextDouble();
        System.out.println("Enter The Second Number: ");
        num2 = in.nextDouble();
        System.out.println("THE RESULT IS :");
        System.out.println(num1   num2   "\n");
    }

    public static void SUBTRACTATION() {
        System.out.println("SUBTRACTION OPERATION Is Selected. \n"
                  "Enter The First Number:  ");
        num1 = in.nextDouble();
        System.out.println("Enter The Second Number: ");
        num2 = in.nextDouble();
        System.out.println("THE RESULT IS :");
        System.out.println(num1 - num2   "\n");
    }

    public static void MULTIPLICATION() {
        System.out.println("MULTIPLICATION OPERATION Is Selected.\n"
                  "Enter The First Number:  ");
        num1 = in.nextDouble();
        System.out.println("Enter The Second Number: ");
        num2 = in.nextDouble();
        System.out.println("THE RESULT IS :");
        System.out.println(num1 * num2   "\n");
    }

    public static void DIVISION() {
        System.out.println("DIVISION OPERATION Is Selected.\n"
                  "Enter The First Number:  ");
        num1 = in.nextDouble();
        System.out.println("Enter The Second Number: ");
        num2 = in.nextDouble();

        if (num2 == 0) {
            System.out.println("INDIVISIBLE BY ZERO \n");
        } else {
            System.out.println("THE RESULT IS :");
            System.out.println(num1 / num2   "\n");
        }
    }

    public static void REMAINDER() {
        System.out.println("REMAINDER OPERATION Is Selected.\n"
                  "Enter The First Number:  ");
        num1 = in.nextDouble();
        System.out.println("Enter The Second Number: ");
        num2 = in.nextDouble();
        System.out.println("THE RESULT IS :");
        System.out.println(num1 % num2   "\n");
    }
    //// the end of binary methods and the beginning of uniary

    public static int sub2() {  // uniary submenu
        String line2 = "UNARY OPERATIONS Are Selected. \n  "
                  "Please Identify Which Operation You Are Going To Use By Entering It's Number. \n\n "
                  "1- NUMBER INCREMENT \n"
                  "2- NUMBER DECREMENT\n"
                  "3- NUMBER SQUARED\n"
                  "4- SQUARE-ROOT \n ";
        System.out.println(line2);

        Z = in.nextInt();
        return Z;
    }

    public static void NUMBERINCREMENT() {
        System.out.println("NUMBER INCREMENT OPERATION Is Selected.\n"
                  "Enter Just One Number:");
        num0 = in.nextDouble();

        System.out.println("THE RESULT IS :");
        System.out.println(  num0   "\n");
    }

    public static void NUMBERDECREMENT() {
        System.out.println("NUMBER DECREMENT OPERATION Is Selected.\n"
                  "Enter Just One Number:");
        num0 = in.nextDouble();

        System.out.println("THE RESULT IS :");
        System.out.println(--num0   "\n");
    }

    public static void NUMBERSQUARED() {
        System.out.println("NUMBER SQUARING OPERATION Is Selected.\n"
                  "Enter Just One Number:");
        num0 = in.nextDouble();

        System.out.println("THE RESULT IS :");
        System.out.println(Math.pow(num0, 2)   "\n");
    }

    public static void SQUARE_ROOT() {
        System.out.println("SQUARE ROOT OPERATION Is Selected.\n"
                  "Enter Just One Number:");
        num0 = in.nextDouble();
        if (num0 < 0) {
            System.out.println("Can not Square Root a Negative Number \n");
        } else {
            System.out.println("THE RESULT IS :");
            System.out.println(Math.sqrt(num0)   "\n");
        }
    }
}

CodePudding user response:

Each if calls main1(), that's why you get the prompt twice. Store it instead to a temporary variable like

int chosenOption = main1();
if (chosenOption == 1)
    ...

or perhaps switch statement. What is important is call it once, store it to a variable.

  •  Tags:  
  • Related