/* example of a menu driven program create a simple calculator that will add sub
ID: 3625381 • Letter: #
Question
/*example of a menu driven program
create a simple calculator that will add subtract multiply and divide
values entered by the user
*/
import java.util.*; //used to include scanner
public class menuExample
{
public static void main(String args[])
{
//declare a scanner for user Input
Scanner userInput = new Scanner(System.in);
int choice;
int value1, value2
double.number; //hold result of integer value
do
{
//display our menu
System.out.println("*** Calculator v1.0***");
System.out.println("1, Addition");
System.out.println("2, Subtration");
System.out.println("3, Multiplication");
System.out.println("4, Division");
System.out.println("5, Modulos");
System.out.println("6. Exit");
System.out.println("**********************");
System.out.println("Please enter your choice:");
//get user input
choice = userInput.nextInt();
//switch the choice from user
switch(choice)
{
case 1://addition
System.out.println("addition");
System.out.println("Please enter two values to be added");
value1 = userInput.nextInt();
value2 = userInput.nextInt();
System.out.println(value1 + " + " + value2 + " = " + (value1+value2);
break;
case 2://subtraction
System.out.println("subraction");
System.out.println("Please enter two values to be subtrated");
value1 = userInput.nextInt();
value2 = userInput.nextInt();
System.out.println(value1 + " - " + value2 + " = " + (value1-value2);
break;
case 3://multiplication
System.out.println("multiplication");
System.out.println("Please enter two values to be multiplicated");
value1= userInput.nextInt();
value2 = userInput.nextInt();
System.out.println(value1 + " * " + value2 + " = " + (value18value2);
break;
case 4://division
System.out.println("division");
System.out.println("Please enter two values to be divided");
value1= userInput.nextInt();
value2 = userInput.nextInt();
number = (double)value1 / value2; //type cast
System.out.println(value1 + " / " + value2 + " = " + number);
break;
case 5://division
System.out.println("multiplication");
System.out.println("Please enter two values to be divided");
value1= userInput.nextInt();
value2 = userInput.nextInt();
System.out.println(value1 + " % " + value2 + " = " + (value1%value2);
break;
case 6://exit
System.out.println("You have chose exit!");
break;
default://default
System.out.println("You entered an invalid choice");
}
}while(choice != 5);
}//main
}//class
Explanation / Answer
please rate - thanks
I changed yours as little as possible
import java.util.*; //used to inlcude scanner
public class menuExample
{
public static void main(String args[])
{
//declare a scanner for user Input
Scanner userInput = new Scanner(System.in);
Random r=new Random();
int choice;
int value1, value2,answer;
double number; //hold the result of integer division
double ans2;
do
{
//display our menu
System.out.println("***Calculator v1.0***");
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.println("5. Modulus");
System.out.println("6. Exit");
System.out.println("*********************");
System.out.println("Please enter your choice:");
//get user input
choice = userInput.nextInt();
//switch the choice from user
switch(choice)
{
case 1://addition
System.out.println("addition");
value1 = r.nextInt(15)+1;
value2 = r.nextInt(15)+1;
System.out.println(value1 + " + " + value2 + " = " );
System.out.println("Please enter your answer:");
answer=userInput.nextInt();
if(answer==value1+value2)
System.out.println("You are correct");
else
System.out.println("You are incorrect");
break;
case 2://subtraction
System.out.println("subtraction");
value1 = r.nextInt(15)+1;
value2 = r.nextInt(15)+1;
System.out.println(value1 + " - " + value2 + " = " );
System.out.println("Please enter your answer:");
answer=userInput.nextInt();
if(answer==value1-value2)
System.out.println("You are correct");
else
System.out.println("You are incorrect");
break;
case 3://multiplication
System.out.println("multiplication");
value1 = r.nextInt(15)+1;
value2 =r.nextInt(15)+1;
System.out.println(value1 + " * " + value2 + " = " );
System.out.println("Please enter your answer:");
answer=userInput.nextInt();
if(answer==value1*value2)
System.out.println("You are correct");
else
System.out.println("You are incorrect");
break;
case 4://division
System.out.println("division");
value1 = r.nextInt(15)+1;
value2 = r.nextInt(15)+1;
number = (double)value1 / value2; //type cast to avoid integer division
System.out.println(value1 + " / " + value2 + " = " );
System.out.println("Please enter your answer:");
ans2=userInput.nextDouble();
if(ans2==number)
System.out.println("You are correct");
else
System.out.println("You are incorrect");
break;
case 5: //modulus
System.out.println("modulus");
value1 = r.nextInt(15)+1;
value2 = r.nextInt(15)+1;
System.out.println(value1 + " % " + value2 + " = " );
System.out.println("Please enter your answer:");
answer=userInput.nextInt();
if(answer==value1%value2)
System.out.println("You are correct");
else
System.out.println("You are incorrect");
break;
case 6://exit
System.out.println("You have chose exit!");
break;
default://default
System.out.println("You entered an invalid choice");
}
}while(choice != 6);
}//main
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.