Explanation
Explanation
CCIS-2B
So I use a new Scanner object with `Scanner scanner = new Scanner(System.in) This object
will be used to gather input from the user. Then the code System.out.println("Enter first number:
") and double num1 = scanner.nextDouble() prints a message to the console asking the user to
enter the first number. The Scanner then captures that number from the user. So I use double
to allow the number to be a decimal number. The same process is repeated for the second
number. With System.out.println("Enter an operator (+, -, *, /): ");` and `char operator =
scanner.next().charAt(0) we ask the user to enter an operator. then close the scanner object
The switch statement performs the operation based on the operator the user entered. If the
operator is not one of the four valid operators (+, -, *, /), an error message is printed to the
output.
This program is a calculator. It helps you do calculation computations with two figures. First, it
asks you to enter the first number. You can enter any number with or without a decimal point.
Also, it asks you to choose an operation like adding, multiplying, or dividing. You can use the
symbols,-, *, or / After that, it asks you to enter the alternate number. you can enter any number
you want. Once you have entered all the figures and chosen an operation, the program will do
the calculation for you and show the result. Still, it will show the answer without any decimal
points, If the result is a whole number(no decimal places). But if the result has decimal places, it
scanner.close();
double output;
switch(operator)
{
case '+':
output = num1 + num2;
break;
case '-':
output = num1 - num2;
break;
case '*':
output = num1 * num2;
break;
case '/':
if (num2 != 0) {
output = num1 / num2;
} else {
out.println("Bawal zero makulet.");
return;
}
break;
default:
out.println("mali operator mo.");
return;
}
}
}