Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I wrote a code for math quiz. case4 is not working properly... import java.util.

ID: 3646780 • Letter: I

Question

I wrote a code for math quiz.
case4 is not working properly...


import java.util.Scanner;

public class lab3MathQuiz {
public static void main(String[] args) {

// two randomly generated numbers
double a = Math.random()*100;
double b = Math.random()*100;
//cast type to int
int ranA = (int) a;
int ranB = (int) b;

//operators
double operator = 4*Math.random()+1;
int oper = (int) operator;

//create Scanner
Scanner input = new Scanner(System.in);

switch (oper){
case 1:
System.out.println(ranA+" + "+ranB+" = ?");
int urAnswer = input.nextInt();
int answer = ranA + ranB;
if (urAnswer==answer){
System.out.println("Answer is correct!");
} else {
System.out.println("Answer is incorrect!" + "The correct answer is " + answer);
}
break;
case 2:
System.out.println(ranA+" - "+ranB+" = ?");
int urAnswer1 = input.nextInt();
int answer1 = ranA - ranB;
if (urAnswer1==answer1){
System.out.println("Answer is correct!");
} else {
System.out.println("Answer is incorrect!" + "The correct answer is " + answer1);
}
break;
case 3:
System.out.println(ranA+" * "+ranB+" = ?");
int urAnswer2 = input.nextInt();
int answer2 = ranA * ranB;
if (urAnswer2==answer2){
System.out.println("Answer is correct!");
} else {
System.out.println("Answer is incorrect!" + "The correct answer is " + answer2);
}
break;
case 4:
System.out.println(ranA+" / "+ranB+" = ?");
int urAnswer3 = input.nextInt();
int answer3 = ranA / ranB;
if (urAnswer3 == answer3){
System.out.println("Answer is correct!");
} else {
System.out.println("Answer is incorrect!" + "The correct answer is " + answer3);
}
break;
default:
}


}
}

Explanation / Answer

this works fine with a conversion of the datatypes precisely.


import java.util.Scanner;

public class lab3MathQuiz {
public static void main(String[] args) {

// two randomly generated numbers
double a = Math.random()*100;
double b = Math.random()*100;
//cast type to int
int ranA = (int) a;
int ranB = (int) b;

//operators

int oper = 4;

//create Scanner
Scanner input = new Scanner(System.in);

switch (oper){
case 1:
System.out.println(ranA+" + "+ranB+" = ?");
int urAnswer = input.nextInt();
int answer = ranA + ranB;
if (urAnswer==answer){
System.out.println("Answer is correct!");
} else {
System.out.println("Answer is incorrect!" + "The correct answer is " + answer);
}
break;
case 2:
System.out.println(ranA+" - "+ranB+" = ?");
int urAnswer1 = input.nextInt();
int answer1 = ranA - ranB;
if (urAnswer1==answer1){
System.out.println("Answer is correct!");
} else {
System.out.println("Answer is incorrect!" + "The correct answer is " + answer1);
}
break;
case 3:
System.out.println(ranA+" * "+ranB+" = ?");
int urAnswer2 = input.nextInt();
int answer2 = ranA * ranB;
if (urAnswer2==answer2){
System.out.println("Answer is correct!");
} else {
System.out.println("Answer is incorrect!" + "The correct answer is " + answer2);
}
break;
case 4:
System.out.println(ranA+" / "+ranB+" = ?");
int urAnswer3 = (int)input.nextDouble();
int answer3 = ranA / ranB;
if (urAnswer3 == answer3){
System.out.println("Answer is correct!");
} else {
System.out.println("Answer is incorrect!" + "The correct answer is " + answer3);
}
break;
default:
}


}
}