2014-02-11 13 views
1

我正在製作數學練習應用程序。這是循環10個隨機問題,並有櫃檯,顯示你有多少正確的,有多少你有錯。無論我的程序如何增加值

我試圖做一些異常處理,以便當沒有值輸入時彈出出現。當彈出窗口出現錯誤答案計數器的計數器或正確答案計數器增加時,無論如何。

這裏是代碼:

import javax.swing.*; 

import java.awt.*; 
import java.awt.event.*; 



@SuppressWarnings("serial") 
public class MathPractice extends JFrame { 
    Container c; 
    JPanel output; 
    JLabel title, num1, num2, lbloperator, equals, mark, display, correct, wrong ,correctN, wrongN; 
    JTextField answer; 
    JButton newB, ansB; 
    JTextArea txtdisplay; 

    String operator; 

    int number1;int number2;int rightNum = 0;int wrongNum = 0;int result;int random;int randomOperator; int cnt = 1; 

    public int getRandom(){ 
     int random = (int)(Math.random() * 10 + 1); 
     return random; 
    } 
    public String getRandomOperator(){ 
     int randomOperator = (int)(Math.random() * 4 + 1); 
     if(randomOperator == 1) 
      return "+"; 
     else if (randomOperator == 2) 
      return "-"; 
     else if (randomOperator == 3) 
      return "*"; 
     else { 
      return "/"; 
     }  
    } 

    class ExcerciseHandler implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 
       number1 = getRandom(); 
       number2 = getRandom(); 

       num1.setText(""+number1); 
       num2.setText(""+number2); 

       operator = getRandomOperator(); 

       lbloperator.setText(operator); 

       rightNum = 0; 
       wrongNum = 0; 
       cnt = 1; 

       correctN.setText("" + rightNum); 
       wrongN.setText("" + wrongNum); 
       display.setText(""); 

       answer.setText(""); 
      } 
     } 



    class AnswerHandler implements ActionListener { 
     public void actionPerformed(ActionEvent e) {    
        boolean canRun = true; 
        double input = 0; 
        try{ 
        input = Double.parseDouble(answer.getText());    
        } 
        catch (Exception ex){ 
         canRun = false; 
         JOptionPane.showMessageDialog(null, "Please enter a valid number!"); 
         answer.requestFocus(); 
         answer.selectAll(); 
        } 
        operator = lbloperator.getText(); 

       if(cnt <= 10){ 
        if (operator.equals("+")){ 
         result = number1 + number2; 
         if(input == result){ 
          rightNum++; 
          correctN.setText(""+rightNum); 
         } 
         else { 
          wrongNum++; 
          wrongN.setText(""+wrongNum); 
         } 
        } 
        else if (operator.equals("-")){ 
         result = number1 - number2; 
         if(input == result){ 
          rightNum++; 
          correctN.setText(""+rightNum); 
         } 
         else { 
          wrongNum++; 
          wrongN.setText(""+wrongNum); 
         } 

        } 
        else if (operator.equals("*")){ 
         result = number1 * number2; 
         if(input == result){ 
          rightNum++; 
          correctN.setText(""+rightNum); 
         } 
         else { 
          wrongNum++; 
          wrongN.setText(""+wrongNum); 
         } 
        } 
        else { 
         result = number1/number2; 
         if(input == result){ 
          rightNum++; 
          correctN.setText(""+rightNum); 
         } 
         else { 
          wrongNum++; 
          wrongN.setText(""+wrongNum); 
         }     
        } 
        number1 = getRandom(); 
        number2 = getRandom(); 

        num1.setText(""+number1); 
        num2.setText(""+number2); 

        lbloperator.setText("" + getRandomOperator()); 

        answer.setText(""); 
        answer.requestFocus(); 

        cnt++; 


       } 
       if (cnt == 11 && rightNum > 6){ 
        display.setText("Keep up the good work!"); 
       } 
       else if (cnt == 11 && rightNum <= 6){ 
        display.setText("Better luck next time!"); 
       } 
      } 
    } 



    Font f = new Font("Arial", Font.BOLD, 20); 
    Font numbers = new Font("Arial", Font.PLAIN, 30); 
    Font operators = new Font("Arial", Font.PLAIN, 20); 
    Font outputF = new Font("Arial", Font.ITALIC, 15); 
    Font fAnswers = new Font("Arial", Font.BOLD, 30); 



    MathPractice(){ 
     super("Math Practice"); 
     c = getContentPane(); 
     c.setLayout(null); 
     //c.setBackground(Color.blue); 

     title = new JLabel("How Much is: "); 
     title.setSize(150,20); 
     title.setLocation(130, 5); 
     title.setFont(f); 
     title.setForeground(Color.red); 

     number1 = getRandom(); 
     num1 = new JLabel(""+ number1); 
     num1.setSize(40, 40); 
     num1.setLocation(30, 35); 
     num1.setFont(numbers); 

     lbloperator = new JLabel(getRandomOperator()); 
     lbloperator.setSize(40, 40); 
     lbloperator.setLocation(85, 37); 
     lbloperator.setFont(operators); 

     number2 = getRandom(); 
     num2 = new JLabel(""+ number2); 
     num2.setSize(40, 40); 
     num2.setLocation(140, 35); 
     num2.setFont(numbers);  

     equals = new JLabel(" = "); 
     equals.setSize(40, 40); 
     equals.setLocation(200, 35); 
     equals.setFont(numbers); 

     answer = new JTextField(8); 
     answer.setSize(80, 40); 
     answer.setLocation(250, 35); 
     answer.setFont(numbers);   

     mark = new JLabel("?"); 
     mark.setSize(40, 40); 
     mark.setLocation(350, 35); 
     mark.setFont(numbers); 

     display = new JLabel(""); 
     display.setSize(350, 40); 
     display.setLocation(30, 90); 
     display.setFont(numbers); 
     //display.setVisible(false); 



     newB = new JButton(" New Excercise "); 
     newB.setSize(170, 40); 
     newB.setLocation(15, 170); 
     ExcerciseHandler eh = new ExcerciseHandler(); 
     newB.addActionListener(eh); 

     ansB = new JButton(" Answer "); 
     ansB.setSize(170, 40); 
     ansB.setLocation(195,170); 
     AnswerHandler ah = new AnswerHandler(); 
     ansB.addActionListener(ah); 

     output = new JPanel(); 
     output.setLayout(null); 
     output.setSize(375, 225); 
     output.setBorder(BorderFactory.createTitledBorder("Statistic: ")); 
     output.setLocation(5, 225); 

     correct = new JLabel("<html> Number of Correct\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Answers: "); 
     correct.setSize(150, 50); 
     correct.setLocation(15, 25); 
     correct.setFont(outputF); 

     wrong = new JLabel("<html> Number of Wrong\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Answers: "); 
     wrong.setSize(150, 50); 
     wrong.setLocation(230, 25); 
     wrong.setFont(outputF); 

     correctN = new JLabel("" + rightNum); 
     correctN.setSize(100, 100); 
     correctN.setLocation(60, 50); 
     correctN.setFont(fAnswers); 
     correctN.setForeground(Color.green); 

     wrongN = new JLabel("" + wrongNum); 
     wrongN.setSize(100, 100); 
     wrongN.setLocation(275, 50); 
     wrongN.setFont(fAnswers); 
     wrongN.setForeground(Color.red);  

     c.add(title);c.add(num1);c.add(lbloperator);c.add(num2);c.add(equals);c.add(answer);c.add(mark);c.add(display);c.add(newB); 
     c.add(ansB);c.add(output); 

     output.add(correct);output.add(wrong);output.add(correctN);output.add(wrongN); 
    } 

    public static void main(String[] args) { 
     MathPractice app = new MathPractice(); 
     app.setSize(400, 500); 
     app.setVisible(true); 
     app.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 

    } 

} 

回答

3

將一個return;聲明你的catch塊的底部。這會導致您的代碼在發生異常時處理異常後離開處理程序。

try{ 
    input = Double.parseDouble(answer.getText());    
} 
catch (NumberFormatException ex){ 
    canRun = false; 
    JOptionPane.showMessageDialog(null, "Please enter a valid number!"); 
    answer.requestFocus(); 
    answer.selectAll(); 

    return; // ****** added ***** 
} 

順便說一句:你應該避免受涼Exception而是應該只趕上具體例外,這裏NumberFormatException

+0

謝謝!-------------- – user278153

+0

@ user278153:不客氣! –

0

顯示錯誤彈出窗口後,該方法繼續執行,因此增加計數器。如果輸入了錯誤的值,則結束該方法,或者循環直到輸入正確的值。