This is another school project where the main class generates two random numbers and then displays them as an addition problem. The user then has to enter the sum of the two numbers. The program should compare the input to the correct answer but every time the correct answer is put in, it says it is wrong?
Main Class:
import java.util.Random;
public class MathQuiz
{
private int answer1, answer2, answer3, answer4, answer5, n1, n2, sum, score, scoreFinal;
private String message, m2, m3, m4, m5, finalPage;
public MathQuiz()
{
}
Random dice1 = new Random();
public void setN1()
{
n1 = dice1.nextInt(100);
}
public void setN2()
{
n2 = dice1.nextInt(100);
}
public void setSum()
{
sum = n1+ n2;
}
public int getN1()
{
return n1;
}
public int getN2()
{
return n2;
}
public int getSum()
{
return sum;
}
public String getAnswer1(String a1)
{
if (a1.equals(getSum()))
{
message = "Correct";
score++;
}
else
{
message = "Incorrect";
}
return message;
}
public String getAnswer2(String a2)
{
if (a2.equals(getSum()))
{
m2 = "Correct";
score++;
}
else
{
m2 = "Incorrect";
}
return m2;
}
public String getAnswer3(String a3)
{
if (a3.equals(getSum()))
{
m3 = "Correct";
score++;
}
else
{
m3 = "Incorrect";
}
return m3;
}
public String getAnswer4(String a4)
{
if (a4.equals(getSum()))
{
m4 = "Correct";
score++;
}
else
{
m4 = "Incorrect";
}
return m4;
}
public String getAnswer5(String a5)
{
if (a5.equals(getSum()))
{
m5 = "Correct";
score++;
}
else
{
m5 = "Incorrect";
}
return m5;
}
public String getLast()
{
scoreFinal = score * 20;
finalPage = "Finished! Your score is " + scoreFinal + "%";
return finalPage;
}
}
JOptionPane:
import javax.swing.JOptionPane;
public class Dialog
{
public static void main(String[] args)
{
String A1, A2, A3, A4, A5;
int a1, a2, a3, a4, a5;
MathQuiz quiz = new MathQuiz();
JOptionPane.showMessageDialog(null, "Answer these 5 math questions...");
quiz.setN1();
quiz.setN2();
quiz.setSum();
A1 = JOptionPane.showInputDialog(quiz.getN1() + " + " + quiz.getN2());
JOptionPane.showMessageDialog(null, quiz.getAnswer1(A1));
quiz.setN1();
quiz.setN2();
quiz.setSum();
A2 = JOptionPane.showInputDialog(quiz.getN1() + " + " + quiz.getN2());
JOptionPane.showMessageDialog(null, quiz.getAnswer1(A2));
quiz.setN1();
quiz.setN2();
quiz.setSum();
A3 = JOptionPane.showInputDialog(quiz.getN1() + " + " + quiz.getN2());
JOptionPane.showMessageDialog(null, quiz.getAnswer1(A3));
quiz.setN1();
quiz.setN2();
quiz.setSum();
A4 = JOptionPane.showInputDialog(quiz.getN1() + " + " + quiz.getN2());
JOptionPane.showMessageDialog(null, quiz.getAnswer1(A4));
quiz.setN1();
quiz.setN2();
quiz.setSum();
A5 = JOptionPane.showInputDialog(quiz.getN1() + " + " + quiz.getN2());
JOptionPane.showMessageDialog(null, quiz.getAnswer1(A5));
JOptionPane.showMessageDialog(null, quiz.getLast());
}
}