/* convert the following program discussed in class, to use if/else if statments
ID: 3625276 • Letter: #
Question
/*convert the following program discussed in class, to use if/else if statments instead of a switch statement:
this is an 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 inlcude 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 the result of integer division
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");
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("subtraction");
System.out.println("Please enter two values to be subtracted:");
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 multiplied:");
value1 = userInput.nextInt();
value2 = userInput.nextInt();
System.out.println(value1 + " * " + value2 + " = " + (value1*value2));
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 to avoid integer division
System.out.println(value1 + " / " + value2 + " = " + number);
break;
case 5: //modulus
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 != 6);
}//main
}//class
Explanation / Answer
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);
int choice,correct=0,incorrect=0,total=0;
int value1, value2,addcount=0,subcount=0,mulcount=0,divcount=0,modcount=0;
double number; //hold the result of integer division
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();
total++;
//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));
correct++;
addcount++;
break;
case 2://subtraction
System.out.println("subtraction");
System.out.println("Please enter two values to be subtracted:");
value1 = userInput.nextInt();
value2 = userInput.nextInt();
System.out.println(value1 + " - " + value2 + " = " + (value1-value2));
correct++;
subcount++;
break;
case 3://multiplication
System.out.println("multiplication");
System.out.println("Please enter two values to be multiplied:");
value1 = userInput.nextInt();
value2 = userInput.nextInt();
System.out.println(value1 + " * " + value2 + " = " + (value1*value2));
correct++;
mulcount++;
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 to avoid integer division
System.out.println(value1 + " / " + value2 + " = " + number);
correct++;
divcount++;
break;
case 5: //modulus
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));
correct++;
modcount++;
break;
case 6://exit
System.out.println("You have chose exit!");
break;
default://default
incorrect++;
System.out.println("You entered an invalid choice");
}
}while(choice != 6);
System.out.println(" Total number of problems attempted:"+total);
System.out.println("Total addition problem attempts:"+addcount);
System.out.println("Total subtraction problem attempts:"+subcount);
System.out.println("Total multiplication problem attempts:"+mulcount);
System.out.println("Total division problem attempts:"+divcount);
System.out.println("Total mod problem attempts:"+modcount);
System.out.println(" Total number of problems attempted:"+incorrect);
System.out.println(" Percentage of correct:"+(correct/total)*100);
}//main
}//class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.