[英]Random Math Java Program Showing Incorrect Answers
我的程序生成radnom数学问题。 Onclick显示了数学问题的答案。
每个答案都是错误的,我无法弄清楚显示为答案的数字的相关性。
这是完整的程序,
驾驶员等级
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;
public class Driver extends MathProblems {
MathProblems problems = new MathProblems();
String s = "Welcome Students!";
String b = "Start!";
private JFrame f;
private JPanel p;
JFrame frame = new JFrame();
JButton b1 = new JButton(b);
JLabel jl = new JLabel(s);
int i;
public Driver () {
gui();
}
public void gui() {
f = new JFrame("Flash Card Program");
p = new JPanel();
f.setLayout( new GridLayout( 2, 1 ) );
f.add(jl);
f.add(p);
p.setLayout( new GridLayout( 2, 1 ) );
p.add(b1);
jl.setHorizontalAlignment(JLabel.CENTER);
// pack the frame for better cross platform support
f.pack();
// Make it visible
f.setVisible(true);
f.setSize(560,400); // default size is 0,0
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
if(b1.getText().equals("Click For Answer"))
{
problems.run();
jl.setText(problems.toString());
b = "Next Question";
b1.setText(b);
}
else
{
problems.run();
jl.setText(problems.getQuestion());
b = "Click For Answer";
b1.setText(b);
}
}
});
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new Driver();
}
});
} // End main Method
} // End class Driver
MathProblems类
import java.util.Random;
public class MathProblems {
private static final int MAX_NUMBER = 10;
private static final Random random = new Random();
private int expected = 0;
private String question = "";
public void run() {
final int a = random.nextInt(MAX_NUMBER);
final int b = random.nextInt(MAX_NUMBER);
final int type = random.nextInt(4);
switch (type) {
case 0:
add(a, b);
break;
case 1:
subtract(a, b);
break;
case 2:
multiply(a, b);
break;
case 3:
divide(a, b);
break;
}
}
private void add(final int a, final int b) {
expected = a + b;
askQuestion(a + " + " + b + " = ");
}
private void subtract(final int a, final int b) {
expected = a - b;
askQuestion(a + " - " + b + " = ");
}
private void multiply(final int a, final int b) {
expected = a * b;
askQuestion(a + " * " + b + " = ");
}
private void divide(final int a, final int b) {
expected = a / b;
askQuestion(a + " / " + b + " = ");
}
private void askQuestion(final String question) {
this.question = question;
}
public String getQuestion() {
return question;
}
public int getAnswer() {
return expected;
}
@Override
public String toString(){
return Integer.toString(expected);
}
}
当您的jframe调用下面的problem.run()时,它将生成一个新问题并显示该问题的答案。
if(b1.getText().equals("Click For Answer"))
{
problems.run();
jl.setText(problems.toString());
b = "Next Question";
b1.setText(b);
}
改变这个
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
if(b1.getText().equals("Click For Answer"))
{
problems.run();
jl.setText(problems.toString());
b = "Next Question";
b1.setText(b);
}
else
{
problems.run();
jl.setText(problems.getQuestion());
b = "Click For Answer";
b1.setText(b);
}
}
});
为此,
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
if(b1.getText().equals("Click For Answer"))
{
jl.setText(problems.toString());
b = "Next Question";
b1.setText(b);
}
else
{
problems.run();
jl.setText(problems.getQuestion());
b = "Click For Answer";
b1.setText(b);
}
}
});
我怀疑问题在这里:
if(b1.getText().equals("Click For Answer")) {
problems.run();
jl.setText(problems.toString());
b = "Next Question";
b1.setText(b);
}
我想您想显示答案,但首先要重新生成问题。 删除对problems.run()
的调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.