Home > Net >  Missing second value memory calculator
Missing second value memory calculator

Time:02-21

My program runs but when I need it to calculate what I need it to do (add, subtract, multiply, divide) it just puts in the value that I typed in and won't do the operation I tell it to do, it just displays the menu again. How do I fix it so it can do the function it needs to do and also loop back to the menu to do another function? (ex. I want to add 2 and 2 together then turn around and multiply 3 and 2)

public static void main(String[] args) {            
    Testt calc = new Testt();
    calc.getCurrentValue();
    displayMenu();
}

public static int displayMenu() {

    Scanner input = new Scanner(System.in);
    Testt calc = new Testt();
    int choice;
    do {
        System.out.println("Hello, welcome to the menu");
        System.out.println("Select one of the following items from the menu:");
        System.out.println("1) Add ");
        System.out.println("2) Subtract ");
        System.out.println("3) Multiply ");
        System.out.println("4) Divide ");
        System.out.println("5) Clear ");
        System.out.println("6.Quit");
        System.out.println ("Please choice an option from the menu:");

        choice = input.nextInt();

        if (choice > 6 || choice < 1) {
            System.out.println("Sorry,"   choice   " was not an option");
            return displayMenu();
        }

    } while (choice > 6 && choice < 1);

    if (choice == 5) {
        calc.clear();
        return 0;
    } else if (choice == 6) {
        System.out.println("Goodbye! ");
        System.exit(0);
    }

    System.out.println("What is the second number? ");
    double operand2 = input.nextDouble();

    switch (choice) {

    case 1:
        calc.add(operand2);
        break;

    case 2:
        calc.subtract(operand2);
        break;

    case 3:
        calc.multiply(operand2);
        break;

    case 4:
        calc.divide(operand2);
        break;

    }

    return choice;
}

public static double getOperand(String prompt) {

    return 0;
}

private double currentValue;

public double getCurrentValue() {
    System.out.println("The current value is "   currentValue);
    return 0;
}

public void add(double operand2) {
    currentValue = currentValue   operand2;
    getCurrentValue();
}

public void subtract(double operand2) {
    currentValue = operand2 - currentValue;
    getCurrentValue();
}

public void multiply(double operand2) {
    currentValue = operand2 * currentValue;
    getCurrentValue();
}

public void divide(double operand2) {
    if (operand2 == 0) {
        System.out.println("Sorry, you can not divide by 0");
    }
    currentValue = operand2 / currentValue;
    getCurrentValue();

}

public void clear() {
    currentValue = 0;
    getCurrentValue();
}   

CodePudding user response:

From getCurrentValue() you are always returning 0. You need to return current value.

Also if you want to loop back to same thing you can put all this in while(true) loop. like this:

   public static int displayMenu() {
        Scanner input = new Scanner(System.in);
        Demo calc = new Demo();
        int choice;
        System.out.println("Hello, welcome to the menu");
        System.out.println("Select one of the following items from the menu:");
        System.out.println("1) Add ");
        System.out.println("2) Subtract ");
        System.out.println("3) Multiply ");
        System.out.println("4) Divide ");
        System.out.println("5) Clear ");
        System.out.println("6.Quit");

        while(true){
            System.out.println ("Please choice an option from the menu:");
            choice = input.nextInt();

            if (choice > 6 || choice < 1) {
                System.out.println("Sorry,"   choice   " was not an option");
                continue;
            }

            if (choice == 5) {
                calc.clear();
                return 0;
            } else if (choice == 6) {
                System.out.println("Goodbye! ");
                System.exit(0);
            }

            System.out.println("What is the second number? ");
            double operand2 = input.nextDouble();

            switch (choice) {

                case 1:
                    calc.add(operand2);
                    break;

                case 2:
                    calc.subtract(operand2);
                    break;

                case 3:
                    calc.multiply(operand2);
                    break;

                case 4:
                    calc.divide(operand2);
                    break;

            }
        }
    }
  • Related