Java 算数测试小程序

TestFrame.java

  
  
  
  
  1. import javax.swing.*;  
  2. import java.awt.FlowLayout;  
  3. import java.awt.event.*;   
  4.  
  5. public class TestFrame extends JFrame implements ActionListener   
  6. {   
  7.        JTextField textOne,textTwo,textResult;   
  8.        JButton getProblem,giveAnwser;   
  9.        JLabel operatorJLabel,message;   
  10.        TestOperator testOperator;   
  11.  
  12.        public TestFrame()   
  13.        {   
  14.             testOperator=new TestOperator();   
  15.             setLayout(new FlowLayout());   
  16.             textOne=new JTextField(10); //创建textOne,其可见字符长是10   
  17.             textTwo=new JTextField(10); //创建textTwo,其可见字符长是10   
  18.             textResult=new JTextField(10); //创建textResult,其可见字符长是10   
  19.             operatorJLabel=new JLabel("+");   
  20.             message=new JLabel("你还没有回答呢");   
  21.             getProblem=new JButton("获取题目");   
  22.             giveAnwser=new JButton("确认答案");   
  23.  
  24.             add(getProblem);   
  25.             add(textOne);   
  26.             add(operatorJLabel);   
  27.             add(textTwo);   
  28.             add(new JLabel("="));   
  29.             add(textResult);   
  30.             add(giveAnwser);   
  31.             add(message);   
  32.  
  33.             textOne.setEditable(false);   
  34.             textTwo.setEditable(false);   
  35.             getProblem.addActionListener(this);//当前窗口注册为getProblem的ActionEvent事件监视器   
  36.             giveAnwser.addActionListener(this);//当前窗口注册为giveAnwser的ActionEvent事件监视器   
  37.             textResult.addActionListener(this);//当前窗口注册为textResult的ActionEvent事件监视器   
  38.        }   
  39.        public void actionPerformed(ActionEvent e)   
  40.       {   
  41.            if(e.getSource()==getProblem) //判断事件源是否是getProblem   
  42.           {   
  43.             int number1=testOperator.giveNumberOne(100);              
  44.             int number2=testOperator.giveNumberTwo(100);              
  45.             String operator=testOperator.giveOperator();               
  46.             textOne.setText(""+number1);               
  47.             textTwo.setText(""+number2);               
  48.             operatorJLabel.setText(operator);               
  49.             message.setText("请回答");              
  50.             textResult.setText(null);   
  51.           }   
  52.          if(e.getSource()==giveAnwser) //判断事件源是否是giveAnwser   
  53.          {   
  54.              String answer=textResult.getText();   
  55.              
  56.             tryint result=Integer.parseInt(answer);   
  57.                   
  58.                 if(testOperator.getRight(result)==true)  
  59.                 {   
  60.                     message.setText("你回答正确");   
  61.                 }   
  62.                 else   
  63.                 {   
  64.                     message.setText("你回答错误");   
  65.                 }   
  66.               }   
  67.             catch(NumberFormatException ex)   
  68.                {   
  69.                     message.setText("请输入数字字符");   
  70.                }   
  71.           }   
  72.     }   
  73. }  

TestOperator.java

  
  
  
  
  1. public class TestOperator   
  2. {   
  3.      int numberOne,numberTwo;   
  4.      String operator="";   
  5.      boolean right;   
  6.  
  7.      //随机生成第一个数  
  8.      public int giveNumberOne(int n)   
  9.      {   
  10.          numberOne=(int)(Math.random()*n)+1//强制转换为int型  
  11.                                              //因为Math.random()返回的为double型  
  12.          return numberOne;   
  13.      }   
  14.  
  15.     //随机生成第二个数  
  16.      public int giveNumberTwo(int n)   
  17.      {   
  18.          numberTwo=(int)(Math.random()*n)+1;   
  19.          return numberTwo;   
  20.       }   
  21.  
  22.      //随机生成运算符号  
  23.       public String giveOperator()   
  24.       {  
  25.          double d=Math.random();   
  26.          if(d>=0&&d<0.25)   
  27.             operator="+";   
  28.  
  29.          else if(d>0.25&&d<=0.5)  
  30.              operator="-";   
  31.            
  32.          else if(d>0.5&&d<=0.75)           
  33.              operator="*";   
  34.            
  35.          else               
  36.              operator="/";   
  37.            
  38.          return operator;   
  39.       }   
  40.  
  41.       //判断测试结果是否正确  
  42.       public boolean getRight(int answer)   
  43.       {   
  44.           if(operator.equals("+"))   
  45.           {  
  46.                if(answer==numberOne+numberTwo)   
  47.                   right=true;   
  48.  
  49.                else   
  50.                   right=false;   
  51.  
  52.            }   
  53.  
  54.            else if(operator.equals("-"))   
  55.           {   
  56.                if(answer==numberOne-numberTwo)   
  57.                   right=true;   
  58.  
  59.                else   
  60.                    right=false;   
  61.            }   
  62.             
  63.            else if(operator.equals("*"))   
  64.            {   
  65.                if(answer==numberOne*numberTwo)   
  66.                   right=true;   
  67.  
  68.                 else   
  69.                    right=false;   
  70.             }   
  71.            else if(operator.equals("/"))   
  72.            {   
  73.                if(answer==numberOne/numberTwo)   
  74.                   right=true;   
  75.  
  76.                 else   
  77.                    right=false;   
  78.             }   
  79.    return right;   
  80.   }   
  81. }   

MainClass.java

  
  
  
  
  1. import javax.swing.JFrame;  
  2. import javax.swing.UIManager;  
  3.  
  4. public class MainClass   
  5. {      
  6.        public static void main(String args[])   
  7.        {      
  8.            try {  
  9.                  UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); //windows界面风格  
  10.             } catch (Exception e) {  
  11.                 e.printStackTrace();  
  12.             }  
  13.            TestFrame frame=new TestFrame();  
  14.            frame.setTitle("算数测试小程序");             
  15.            frame.setBounds(100,100,500,100);   
  16.            frame.setVisible(true);   
  17.            frame.setLocationRelativeTo(null);//窗体居中显示  
  18.            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  19.            frame.setResizable(false);  
  20.        }   
  21. }  

效果图:

 

 

你可能感兴趣的:(java,代码,小工具,程序,算数的测试)