使用JFrame的JAVA贷款计算器的例外情况?

时间:2012-07-10 02:17:10

标签: 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

我时间紧张,所以任何帮助都会非常感激。

3 个答案:

答案 0 :(得分:2)

您正试图从刚刚创建的字段中获取空白。

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

所以在这里你只是从错误的地方检查。 看起来像第一步你要创建文本字段,第二行应该在其他事件。

希望有所帮助

答案 1 :(得分:1)

   String answerStr = mathAnswers[i].getText();

    if(answerStr.isEmpty()){

          correctYesNo[i] = new JLabel("Not a valid answer/answer field empty!");
    } else {

         answer = Integer.parseInt(answerStr);
         correctAnswer = X * Y;
         //Rest of your code goes here
    }

希望这能帮到你!

答案 2 :(得分:0)

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

导致错误的是什么。这是因为您的某个文本字段为空。在接受用户输入之前进行一些验证,除非您希望应用程序出错。