Answered step by step
Verified Expert Solution
Question
1 Approved Answer
How do I fix this Java error? My objective is to build a basic Java calculator for 2 numbers. I got errors in else if
How do I fix this Java error?
My objective is to build a basic Java calculator for 2 numbers. I got errors in else if statement.
import java.util.Scanner; public class Calculator { public static void main(String[] args) { double firstNumber; double secondNumber; //String operator; Scanner Calculator = new Scanner(System.in); System.out.println("Enter your first number"); firstNumber = Calculator.nextDouble(); System.out.println("Enter your second number"); secondNumber = Calculator.nextDouble(); //char operator = Calculator.next().charAt(0); Scanner operator = new Scanner(System.in); System.out.println("Please enter the operator + - * / "); String op = operator.nextLine(); if (operator == '+') System.out.println(firstNumber + secondNumber); else if (operator == "-") System.out.println(firstNumber - secondNumber); else if (op == '*') System.out.println(firstNumber * secondNumber); else if (operator = "/") { System.out.println(firstNumber / secondNumber); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started