Home > OS >  Why is my program not quitting when I ask it to?
Why is my program not quitting when I ask it to?

Time:10-12

I created a 4-function calculator. Everything works except for when I ask it to quit, it will not, it just continues asking for arg1 and arg2. I set up a while loop to get it to continuously run, however it should exit the program when I type "quit". What am I doing wrong?

import java.util.Scanner;

public class Calc{
    public static void main(String[] args) { 
        Scanner scan = new Scanner(System.in);
   boolean done = true;
  while (done == true) {
        System.out.println("Enter operator:   - * / or quit: "); //gets operator
    String operator = scan.next();

    System.out.println("Enter arg1: "); //gets the first argument
    double arg1 = scan.nextDouble();

    System.out.println("Enter arg2: "); //gets the second argument
    double arg2 = scan.nextDouble();

    if (operator.equals(" ")) {
        add(arg1, arg2);
    }
    if (operator.equals("-")) {
        minus(arg1, arg2);
    }
    if (operator.equals("*")) {
        multiply(arg1, arg2);
    }
    if (operator.equals("/")) {
        divide(arg1, arg2);

    }
    if (operator.equals("quit")) {
        System.out.println("Thank you for using the CSC 220 calculator!");
        done = false;

    }
    if((!operator.equals(" ")) && (!operator.equals("-") && (!operator.equals("/")) && (!operator.equals("*")) && (!operator.equals("quit")))) {
        System.out.println("Error. That is not a supported operator.");
    }
    
}
}
    static void divide (double arg1, double arg2){
        System.out.println(arg1   "/"   arg2   " = "   (arg1 / arg2));
    

    }

    static void multiply (double arg1, double arg2){
        System.out.println(arg1   " * "   arg2   " = "   (arg1 * arg2));
    

    }

    static void add (double arg1, double arg2){
        System.out.println(arg1   "   "   arg2   " = "   (arg1   arg2));
    

    }

    static void minus (double arg1, double arg2){
        System.out.println(arg1   " - "   arg2   " = "   (arg1 - arg2));

    }
}

CodePudding user response:

Please be careful with the order of your if-statements. If you don’t want to ask the user to enter arg1 and arg2, make sure to check if the operator is quit before asking for the user input. As mentioned in a comment, using the debugger of your IDE in combination with debug messages, might help understand better why a certain behavior is occuring.

On top of that, it might be helpful to take a look at the break; statement in the JavaDocs.

CodePudding user response:

You should break the loop after taking the operator input, if the input is quit.

import java.util.Scanner;

public class Calc{
    public static void main(String[] args) { 
        Scanner scan = new Scanner(System.in);
   boolean done = true;
  while (done == true) {
        System.out.println("Enter operator:   - * / or quit: "); //gets operator
    String operator = scan.next();
    if(operator.equals("quit")) {
        break;
    }

    System.out.println("Enter arg1: "); //gets the first argument
    double arg1 = scan.nextDouble();

    System.out.println("Enter arg2: "); //gets the second argument
    double arg2 = scan.nextDouble();

    if (operator.equals(" ")) {
        add(arg1, arg2);
    }
    if (operator.equals("-")) {
        minus(arg1, arg2);
    }
    if (operator.equals("*")) {
        multiply(arg1, arg2);
    }
    if (operator.equals("/")) {
        divide(arg1, arg2);
    }
    if((!operator.equals(" ")) && (!operator.equals("-") && (!operator.equals("/")) && (!operator.equals("*")) && (!operator.equals("quit")))) {
        System.out.println("Error. That is not a supported operator.");
    }
    
}
}
    static void divide (double arg1, double arg2){
        System.out.println(arg1   "/"   arg2   " = "   (arg1 / arg2));
    

    }

    static void multiply (double arg1, double arg2){
        System.out.println(arg1   " * "   arg2   " = "   (arg1 * arg2));
    

    }

    static void add (double arg1, double arg2){
        System.out.println(arg1   "   "   arg2   " = "   (arg1   arg2));
    

    }

    static void minus (double arg1, double arg2){
        System.out.println(arg1   " - "   arg2   " = "   (arg1 - arg2));

    }
}

CodePudding user response:

I agree, that using break is the better way of doing it. However I am a bit confused by why it does not work for you, as the following code exits when you type in "quit", and doesn´t seem to differ from what you provided.

    Scanner scan = new Scanner(System.in);
    String operator;
    boolean quit = false;
    
    while (quit == false) {
        
        operator = scan.next();

        if (operator.equals("quit")) {quit = true;}

    }
  • Related