java - 使用 JFrame 的 JAVA 贷款计算器出现异常?

标签 java swing exception jframe calculator

我想如果我能摆脱程序在尝试运行它时给我的一些异常,我就几乎得到了它。这是我的代码:

package RandomMathGame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class RandomMathGame {

    public static void main(String[] args) {
        RandomProblemGenerator randomProblems = new RandomProblemGenerator(10);
        final int numberProblems = 10;
        int correctScore = 0;
        JPanel panel = new JPanel();
       int answer;
        int correctAnswer;
        JLabel[] mathProblems = new JLabel[numberProblems];
        final JTextField[] mathAnswers = new JTextField[numberProblems];
        JLabel[] correctYesNo = new JLabel[numberProblems];
        final JLabel score = new JLabel(correctScore + "/10");
        JButton submit = new JButton("Submit");
        for (int i = 1; i <= numberProblems; i++)
        {
            final int X = randomProblems.createNumberX();
            final int Y = randomProblems.createNumberY();

            mathProblems[i] = new JLabel("" + X + " * " + Y + " = ");
            mathAnswers[i] = new JTextField();


             answer = Integer.parseInt(mathAnswers[i].getText());
             correctAnswer = X * Y;

            if (answer == correctAnswer)
            {
                correctYesNo[i] = new JLabel("Correct answer; good job!");
                correctScore = correctScore + 1;
            }
            else
            {
               correctYesNo[i] = new JLabel("Incorrect answer; try again!");

            }
             panel.add(mathProblems[i]);
             panel.add(mathAnswers[i]);
             panel.add(correctYesNo[i]);
            }

        final int temp = correctScore;
      submit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                score.setText("Score: " + temp + "/10");
            }
        });



        panel.add(submit);
        panel.add(score);

        JFrame gameFrame = new JFrame();
        gameFrame.setTitle("Random Math Game");
        gameFrame.setSize(150, 150);
        gameFrame.setVisible(true);
        gameFrame.setContentPane(panel);



        }
  }

这些是我运行它时给出的异常:

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:504)
    at java.lang.Integer.parseInt(Integer.java:527)
    at RandomMathGame.RandomMathGame.main(RandomMathGame.java:33)
Java Result: 1

我时间紧迫,因此我们将不胜感激。

最佳答案

您正在尝试从刚刚创建的空白字段中获取数据。

mathAnswers[i] = new JTextField();
answer = Integer.parseInt(mathAnswers[i].getText());

所以在这里你只是从错误的地方检查。 似乎在第一步中您想要创建文本字段,第二行应该在其他事件中出现。

希望对你有帮助

关于java - 使用 JFrame 的 JAVA 贷款计算器出现异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11405753/

相关文章:

java - Jetty HTTP 客户端摘要验证

java - 在java中将jpanel添加到jpanel中

java - Rpg 调用 Java 构造函数,在异常处理程序上出现错误 ClassDefNotFound

java - 如何在java中的其他组件上添加透明矩形面板,就像我们在按下鼠标时选择任何内容一样

java - 包中抛出特定异常的所有方法的列表

c# - "Parameter is not valid."使用保存位图时

java - 如何在客户端服务中动态加载SPRING_PROFILES_ACTIVE值?

java - 将对象与对象的数组列表进行比较

java - 如何调用 EJB 生命周期方法

java - propertyChange Support 与 EventListenerList 的差异以及何时更喜欢它们?