i got the code to work where it repeats if the user answers question incorrectly
ID: 3645916 • Letter: I
Question
i got the code to work where it repeats if the user answers question incorrectly but not if you answer incorrectly it keeps repeating same question and does not accept new answer code is belowimport javax.swing.JOptionPane;
import javax.swing.*;
import java.util.Scanner;
public class Assignment4{
public static void main(String[] args) {
int sum =0;
// Keep reading data until the user answers No
int option = JOptionPane.YES_OPTION;
while (option == JOptionPane.YES_OPTION) {
// will display random numbers up to 12
int number1= (int)(Math.random() * 12);
int number2= (int)(Math.random() * 12);
String choice=JOptionPane.showInputDialog("enter '1' for Addition,'2' for multiplication,'3' for subtraction");
if(choice.equals("2"))
{
// will ask user to answer the total of the 2 random numbers
String dataString = JOptionPane.showInputDialog ("What is "+number1 + " * " +number2 + " ? ");
sum= number1 * number2;
Scanner input = new Scanner(System.in);
int data = Integer.parseInt(dataString);
sum = data;
// this will check to see if answer is correct or incorrect
// will display message based on answer
while ( number1 * number2!= sum)
{
JOptionPane.showMessageDialog(null, "You are incorrect");
dataString = JOptionPane.showInputDialog ("What is "+number1 + " * " +number2 + " ? ");
JOptionPane.showMessageDialog(null, "You answered " + sum + " which is correct ");
}
// will ask user if they wish to continue after they have answered the question
option = JOptionPane.showConfirmDialog(null, "Would you like another question?");
}
else if(choice.equals("3"))
{
// will ask user to answer the total of the 2 random numbers
String dataString = JOptionPane.showInputDialog ("What is "+number1 + "- " +number2 + " ? ");
int diff= number1-number2;
Scanner input = new Scanner(System.in);
int data = Integer.parseInt(dataString);
diff=data;
// this will check to see if answer is correct or incorrect
// will display message based on answer
while ( number1 - number2!= diff)
{
JOptionPane.showMessageDialog(null, "You are incorrect");
dataString = JOptionPane.showInputDialog ("What is "+number1 + " - " +number2 + " ? ");
JOptionPane.showMessageDialog(null, "You answered " + diff + " which is correct ");
}
// will ask user if they wish to continue after they have answered the question
option = JOptionPane.showConfirmDialog(null, "Would you like another question?");
}
else if(choice.equals("1"))
{
// will ask user to answer the total of the 2 random numbers
String dataString = JOptionPane.showInputDialog ("What is "+number1 + " + " +number2 + " ? ");
sum= number1 + number2;
Scanner input = new Scanner(System.in);
int data = Integer.parseInt(dataString);
sum = data;
// this will check to see if answer is correct or incorrect
// will display message based on answer
while ( number1 + number2!=sum )
{
JOptionPane.showMessageDialog(null, "You are incorrect");
dataString = JOptionPane.showInputDialog ("What is "+number1 + " + " +number2 + " ? ");
JOptionPane.showMessageDialog(null, "You answered " + sum + " which is correct ");
}
// will ask user if they wish to continue after they have answered the question
option = JOptionPane.showConfirmDialog(null, "Would you like another question?");
}
}
}
}
Explanation / Answer
please rate - thanks
import javax.swing.JOptionPane;
import javax.swing.*;
import java.util.Scanner;
public class Assignment4{
public static void main(String[] args) {
int sum =0;
// Keep reading data until the user answers No
int option = JOptionPane.YES_OPTION;
while (option == JOptionPane.YES_OPTION) {
// will display random numbers up to 12
int number1= (int)(Math.random() * 12);
int number2= (int)(Math.random() * 12);
String choice=JOptionPane.showInputDialog("enter '1' for Addition,'2' for multiplication,'3' for subtraction");
if(choice.equals("2"))
{
// will ask user to answer the total of the 2 random numbers
String dataString = JOptionPane.showInputDialog ("What is "+number1 + " * " +number2 + " ? ");
sum= number1 * number2;
Scanner input = new Scanner(System.in);
int data = Integer.parseInt(dataString);
// this will check to see if answer is correct or incorrect
// will display message based on answer
while ( number1 * number2!=data )
{
JOptionPane.showMessageDialog(null, "You answered " + data + " which is incorrect ");
dataString = JOptionPane.showInputDialog ("What is "+number1 + " * " +number2 + " ? ");
data = Integer.parseInt(dataString);
}
JOptionPane.showMessageDialog(null, "You answered " + data + " which is correct ");
// will ask user if they wish to continue after they have answered the question
option = JOptionPane.showConfirmDialog(null, "Would you like another question?");
}
else if(choice.equals("3"))
{
// will ask user to answer the total of the 2 random numbers
String dataString = JOptionPane.showInputDialog ("What is "+number1 + "- " +number2 + " ? ");
int diff= number1-number2;
Scanner input = new Scanner(System.in);
int data = Integer.parseInt(dataString);
// this will check to see if answer is correct or incorrect
// will display message based on answer
while ( number1 - number2!=data )
{
JOptionPane.showMessageDialog(null, "You answered " + data + " which is incorrect ");
dataString = JOptionPane.showInputDialog ("What is "+number1 + " - " +number2 + " ? ");
data = Integer.parseInt(dataString);
}
JOptionPane.showMessageDialog(null, "You answered " + data + " which is correct ");
// will ask user if they wish to continue after they have answered the question
option = JOptionPane.showConfirmDialog(null, "Would you like another question?");
}
else if(choice.equals("1"))
{
// will ask user to answer the total of the 2 random numbers
String dataString = JOptionPane.showInputDialog ("What is "+number1 + " + " +number2 + " ? ");
sum= number1 + number2;
Scanner input = new Scanner(System.in);
int data = Integer.parseInt(dataString);
// this will check to see if answer is correct or incorrect
// will display message based on answer
while ( number1 + number2!=data )
{
JOptionPane.showMessageDialog(null, "You answered " + data + " which is incorrect ");
dataString = JOptionPane.showInputDialog ("What is "+number1 + " + " +number2 + " ? ");
data = Integer.parseInt(dataString);
}
JOptionPane.showMessageDialog(null, "You answered " + data + " which is correct ");
// will ask user if they wish to continue after they have answered the question
option = JOptionPane.showConfirmDialog(null, "Would you like another question?");
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.