Now change the menuExample problem to the following: - keep track of total numbe
ID: 3625783 • Letter: N
Question
Now change the menuExample problem to the following:
- keep track of total number of problems attempted.
- keep track of total of each kind of problem attempted
- keep track of total correct/incorrect for each problem type
- keep track of total correct/incorrect problems attempted
when user exits program: display all of the above information and show a percentage of the total correct answers out of all problems attempted.
/*example of a menu driven program
create a simple calculator that will add subtract multiply and dividevalues 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 + " = " + (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 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
import java.util.*;
public class menuExample
{
public static void main(String args[])
{
Scanner userInput = new Scanner(System.in);
Random r=new Random();
int choice;
int problems=0,c1=0,w1=0,c2=0,w2=0,c3=0,w3=0,c4=0,w4=0,c5=0,w5=0;
int value1, value2,answer;
double number; //hold the result of integer division
double ans2;
do
{
//display our menu
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();
problems++;
//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");
c1++;
}
else
{System.out.println("You are incorrect");
w1++;
}
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");
c2++;
}
else
{System.out.println("You are incorrect");
w2++;
}
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");
c3++;
}
else
{System.out.println("You are incorrect");
w3++;
}
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");
c4++;
}
else
{System.out.println("You are incorrect");
w4++;
}
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");
c5++;
}
else
{System.out.println("You are incorrect");
w5++;
}
break;
case 6://exit
problems--;
System.out.println("You have chose exit!");
break;
default://default
problems--;
System.out.println("You entered an invalid choice");
}
}while(choice != 6);
System.out.println("SUMMARY");
System.out.println("Total number of problems "+problems);
System.out.println("Total number of correct problems "+(c1+c2+c3+c4+c5));
System.out.println("Total number of incorrect problems "+(w1+w2+w3+w4+w5));
System.out.println("Total number of addition problems "+(c1+w1));
System.out.println("Total number of correct addition problems "+c1);
System.out.println("Total number of incorrect addition problems "+w1);
System.out.println("Total number of subtraction problems "+(c2+w2));
System.out.println("Total number of correct subtraction problems "+(c2));
System.out.println("Total number of incorrect subtraction problems "+(w2));
System.out.println("Total number of multiplication problems "+(c3+w3));
System.out.println("Total number of correct multiplication problems "+(c3));
System.out.println("Total number of incorrect multiplication problems "+(w3));
System.out.println("Total number of division problems "+(c4+w4));
System.out.println("Total number of correct division problems "+(c4));
System.out.println("Total number of incorrect division problems "+(w4));
System.out.println("Total number of modulus problems "+(c5+w5));
System.out.println("Total number of correct modulus problems "+(c5));
System.out.println("Total number of incorrect modulus problems "+(c5));
}//main
}//class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.