[Solved]Java Need Help Simple Calculator Could Please Edit Following Code 1 User Choose 1 2 3 4 0 Q37214464
[JAVA] Need help with a simple calculator.
Could you please edit the following code so that
1.) If the user does not choose 1, 2, 3, 4 or 0 when choosingwhat the calculation should be performed that the program willrepeat the question until the user inputs a valid number.
2.)This is not necessary, but if at all possible I would greatlyappreciate a difference in text depending on what the user chose.For example, if the user chose addition the phrase would be “Pleasechoose the first number” and the next line would say “What numberwould you like to add to x” with x being the previously chosennumber. Or just a simple “What number would you like added tothat?”
Of course with something like multiplication I would like thetext to change to “What would you like to multiply ‘x’ by?”
Lastly, just a simple message stating what the result is.
Thank you very much!
import java.util.*;
public class NewMain
{
public static void main(String[] args) {
//variable declare
double number1,number2,answer=0;
int choice;
//scanner to get input from user
Scanner sc = new Scanner(System.in);
System.out.println(“***Welcome to the simple calculator!***n——————————————–“);
do{
//ask user to input number
System.out.println(“Please select one of the following optionsn1)Addtionn2)Subtractionn3)Multiplicationn4)Divisionn0)Exit”);
choice = sc.nextInt();
//condition to exit the do while loop
if(choice == 0){
break;
}
//ask user to enter the choice
System.out.println(“Please enter the first number”);
number1 = sc.nextDouble();
System.out.println(“Please enter the second number”);
number2 = sc.nextDouble();
//switch condition to loop the choice
switch(choice){
case 1 : answer = calcSum(number1,number2);break;
case 2 : answer = calcSub(number1,number2);break;
case 3 : answer = calcMult(number1,number2);break;
case 4 : answer = calcDiv(number1,number2);break;
default : System.out.println(“Please enter correctchoice”);break;
}
//print the result after every iteration
displayResult(answer);
}while(choice>0&&choice<5);
}
//calculate sum
static double calcSum(double a, double b){
return a+b;
}
//subtraction
static double calcSub(double a, double b){
return a-b;
}
static double calcMult(double a, double b){
return a*b;
}
//division
static double calcDiv(double a, double b){
return a/b;
}
//print result
static void displayResult(double result){
System.out.println(“The answer is “+result);
}
}
Expert Answer
Answer to [JAVA] Need help with a simple calculator. Could you please edit the following code so that 1.) If the user does not cho… . . .
OR

