0

So I'm writing a code for a calculator in Java as a homework and I wanted to include an else statement in the end where if the user didn't input a valid operator, you get an error msg. Problem is that even when I input a valid operator, it prints the result and then the else error msg. Any help would be appreciated.


public class Main {

    public static void main(String[] args){

        Scanner inputi=new Scanner(System.in);
        double num1,num2;
        char operator;

        System.out.println("Please enter the first number: ");
        num1=inputi.nextDouble();
        System.out.println("Please enter the second number: ");
        num2=inputi.nextDouble();

        Scanner oper=new Scanner(System.in);
        System.out.println("Please enter the operator, the operators are +,-,*,/,%");
        operator = oper.next().charAt(0);

        if(operator== '+') { 
            System.out.println("The sum of these numbers is: "+(num1+num2));
        }
        if(operator=='-') {
            System.out.println("The diff of these numbers is: "+ (num1-num2));       
        }
        if(operator=='*') {
            System.out.println("The result of this multiplication is: "+(num1*num2));
        }
        if(operator=='/') {
            System.out.println("The division is: "+(num1/num2));
        }
        if(operator=='%') {
            System.out.println("The module is: "+(num1%num2));    
        }             
        else {
            System.out.println("Please just pick an operator..");

        }
   }

}
0

4 Answers 4

1

I'd recommend using switch instead:

     switch (operator) {
        case '+':
            System.out.println("+");
            break;
        case '-':
            System.out.println("-");
            break;
    /* Other operators */
        default:
            System.out.println("Please just pick an operator..");
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You have "if if if if else" you want "if else if else if else". Your one else currently only applies to the if (operator == '%')

1 Comment

Oh, I didn't know that. The code works great now, so very kind. Thanks a bunch :]
0

Try 'else if':

 if(operator== '+') { 
            System.out.println("The sum of these numbers is: "+(num1+num2));
        }
else if(operator=='-') {
            System.out.println("The diff of these numbers is: "+ (num1-num2));       
        }
else if(operator=='*') {
            System.out.println("The result of this multiplication is: "+(num1*num2));
        }
else if(operator=='/') {
            System.out.println("The division is: "+(num1/num2));
        }
else if(operator=='%') {
            System.out.println("The module is: "+(num1%num2));    
        }             
        else {
            System.out.println("Please just pick an operator..");

However, a switch statement would be clearer:

switch(operator) {
 case '%': 
  System.out.println("The module is: "+(num1%num2));    
  break; 
 default:
  System.out.println("Please just pick an operator..");
}

Comments

0

The problem here is that you are not fully understanding the if else-if else statements.

The "If" means: if this conditional statement is true then execute the code to follow in the brackets.

The "If-else" means: if the statements above (if and else-if) are not true then execute this code.

The "else" means: if the preceding statement above (if or else-if) are not true then execute this code.

Given this, I'm sure not that you can see your error. All of your "if" statements are evaluating to if this condition then do this. Whereas you really wanted, to use "else-if" where if this is true then only execute the following code with the exclusion of the rest of the conditionals. You are correct in having an else clause pick up the default, however since you are not using "else-if" it really is compiling down to if this statement is not true, (the statement right before your else clause):

    if(operator=='%') {
        System.out.println("The module is: "+(num1%num2));    
    }            

Then execute the else statement, which is not what you want.

I would recommend really figuring out what your code actually compiles down to in terms of machine instructions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.