Home > Software design >  Does anyone know why this isnt working properly?
Does anyone know why this isnt working properly?

Time:02-01

I have written a calculator program.

At the end of this code the user is asked if they would like to use the calculator again. If the user enters 1 for yes, the method greetingChoice() is called which just begins the program again. When 2 for no (2 == "no") is entered the program should print "terminating..." and nothing should happen after.

however when you input 1 for yes and then at the end of that loop input 2 for no, instead of the program just printing "terminating..." like before, it also asks the user if they want to play again. Why is this happening?

im sorry if i havent explained this very well i hope you can help.

import java.util.Scanner;

public class UserChoice {

public static void main(String[] args) {

    greetingChoice();

}

public static void greetingChoice() {
    System.out.println("Please choose one");
    System.out.println(
            "Addition = 1, Subtraction = 2, Multiplication = 3, Division = 4, Exponentiation = 5, Modulus = 6");
    Scanner choice = new Scanner(System.in);
    int type = choice.nextInt();

    if (type == 1) {
        System.out.println("Addition");
        addition();
    } else if (type == 2) {
        System.out.println("Subtraction");
        subtraction();
    } else if (type == 3) {
        System.out.println("Multiplication");
        multiplication();
    } else if (type == 4) {
        System.out.println("Division");
        division();
    } else if (type == 5) {
        System.out.println("Exponentiation");
        exponentiation();
    } else if (type == 6) {
        System.out.println("Modulus");
        modulus();
    }

    playAgain();

}

public static void addition() {
    System.out.println("Please enter the first number: ");
    Scanner scan = new Scanner(System.in);
    int num1 = scan.nextInt();

    System.out.println();
    System.out.println("Please enter the second number: ");
    Scanner scan2 = new Scanner(System.in);
    int num2 = scan2.nextInt();

    int total = num1   num2;
    System.out.println();
    System.out.println(num1   " "   " "   " "   num2   " "   "="   " "   total);
}

public static void subtraction() {
    System.out.println("Please enter the first number: ");
    Scanner scan = new Scanner(System.in);
    int num1 = scan.nextInt();

    System.out.println();
    System.out.println("Please enter the second number: ");
    Scanner scan2 = new Scanner(System.in);
    int num2 = scan2.nextInt();

    int total = num1 - num2;
    System.out.println();
    System.out.println(num1   " "   "-"   " "   num2   " "   "="   " "   total);
}

public static void multiplication() {
    System.out.println("Please enter the first number: ");
    Scanner scan = new Scanner(System.in);
    int num1 = scan.nextInt();

    System.out.println();
    System.out.println("Please enter the second number: ");
    Scanner scan2 = new Scanner(System.in);
    int num2 = scan2.nextInt();

    int total = num1 * num2;
    System.out.println();
    System.out.println(num1   " "   "*"   " "   num2   " "   "="   " "   total);
}

public static void division() {
    System.out.println("Please enter the first number: ");
    Scanner scan = new Scanner(System.in);
    float num1 = scan.nextInt();

    System.out.println();
    System.out.println("Please enter the second number: ");
    Scanner scan2 = new Scanner(System.in);
    float num2 = scan2.nextInt();

    float total = num1 / num2;
    System.out.println();
    System.out.println(num1   " "   "/"   " "   num2   " "   "="   " "   total);
}

public static void exponentiation() {
    System.out.println("Please enter the first number: ");
    Scanner scan = new Scanner(System.in);
    int num1 = scan.nextInt();

    System.out.println();
    System.out.println("Please enter the second number: ");
    Scanner scan2 = new Scanner(System.in);
    int num2 = scan2.nextInt();

    int total = (int) Math.pow(num1, num2);
    System.out.println();
    System.out.println(num1   " "   "**"   " "   num2   " "   "="   " "   total);
}

public static void modulus() {
    System.out.println("Please enter the first number: ");
    Scanner scan = new Scanner(System.in);
    int num1 = scan.nextInt();

    System.out.println();
    System.out.println("Please enter the second number: ");
    Scanner scan2 = new Scanner(System.in);
    int num2 = scan2.nextInt();

    int total = num1 % num2;
    System.out.println();
    System.out.println(num1   " "   "%"   " "   num2   " "   "="   " "   total);
}

public static void playAgain() {

    Scanner scan = new Scanner(System.in);

    while (true) {
        System.out.println("Would you like to use the calculator again? yes = 1, no = 2");
        int input = scan.nextInt();
        if (input == 1) {
            System.out.println("Restarting Calculator...");
            System.out.println("------------------------------------------------------");
            greetingChoice();
        } else {
            System.out.println("Terminating...");
            break;
        }
        

    }
}

}

CodePudding user response:

The issue is in method playAgain().
You have while(true) loop where you are calling greetingChoice().
It creates a stack of methods calling again and again without finishing one.
If given 1 for continuing, it calls greetingChoice() which will call playAgain().
Then given 2 for terminating, it terminates the 2nd call of playAgain(), not the 1st call. 1st call of playAgain() is still in loop. So it asks if want to use calculator again.

You can make few changes to greetingChoice() and playAgain():

public static void greetingChoice() {
    boolean flag = true;
    while(flag){
        .... // pretty much same code.
        flag = playAgain();
    }
}

playAgain(); returns true for 1 and false for anything else.
This way the method finishes it's task instead of adding another one on stack.

public static boolean playAgain() {
    Scanner scan = new Scanner(System.in);
    System.out.println("Would you like to use the calculator again? yes = 1, no = 2");
    int input = scan.nextInt();
    if (input == 1) {
        System.out.println("Restarting Calculator...");
        System.out.println("------------------------------------------------------");
        return true;
    }
    System.out.println("Terminating...");
    return false;
}

CodePudding user response:

There are ways to improve your code, but to address your immediate issue - use a do-while loop. This will mean that the code will be executed at least once before checking the condition to break. So now here we will complete the while loop once every run of the program.

Something like:

do{
//your logic here
} while(input == 1)

CodePudding user response:

Try closing the scanner before breaking the while loop:

System.out.println("Terminating...");
scan.close();
break;
  •  Tags:  
  • Related