これは別の学校のプロジェクトで、メイン クラスで 2 つの乱数を生成し、それらを足し算の問題として表示します。次に、ユーザーは 2 つの数値の合計を入力する必要があります。プログラムは入力を正しい答えと比較する必要がありますが、正しい答えが入力されるたびに、それは間違っていると言われますか?
メインクラス:
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());
}
}