首页 > 代码库 > 四则运算

四则运算

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class B extends JFrame implements ActionListener{
 int max=20;//这里小学四则运算题数量 如果希望出300道题,把max的值改成300即可.
 JLabel jlTitle=new JLabel();
 JLabel jlTotal=new JLabel("共"+max+"题");
 JLabel jl=new JLabel("");
 JLabel jlcorrect=new JLabel();
 JTextField jtf=new JTextField(3);
 JButton jb1=new JButton("上一题");
 JButton jb2=new JButton("下一题");
 JButton jb3=new JButton("交卷");
 JPanel jp1=new JPanel();
 JPanel jp2=new JPanel();
 JPanel jp3=new JPanel();
 String[] question=new String[max];
 int[] answer=new int[max];
 String[] studentAnswer=new String[max];
 boolean[]correct=new boolean[max];
 int count=1;
 boolean submitFlag=false;
 B(){
  super("小学算术测试");
  jlTitle.setFont(new Font(null,Font.PLAIN,20));
  jlTotal.setFont(new Font(null,Font.PLAIN,20));
  jl.setFont(new Font(null,Font.PLAIN,20));
  jlcorrect.setFont(new Font(null,Font.PLAIN,20));
  jlcorrect.setForeground(Color.RED);
  jtf.setFont(new Font(null,Font.PLAIN,20));
  for(int i=0;i<max;i++){
   String s=randomQuestion(100);
   question[i]=s.substring(0,s.indexOf("=")+1);
   answer[i]=Integer.parseInt(s.substring(s.indexOf("=")+1));
  }
  jlTitle.setText("第"+count+"题");
  jl.setText(question[0]);
  jlcorrect.setText("");
  jb1.addActionListener(this);
  jb2.addActionListener(this);
  jb3.addActionListener(this);
  jp1.add(jlTitle);jp1.add(jlTotal);jp1.add(jb3);
  jp2.add(jl);jp2.add(jtf);jp2.add(jlcorrect);
  jp3.add(jb1);jp3.add(jb2);
  add(jp1,BorderLayout.NORTH);
  add(jp2,BorderLayout.CENTER);
  add(jp3,BorderLayout.SOUTH);
  setSize(300, 160);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setLocationRelativeTo(null);
  setVisible(true);
 }
 @Override
 public void actionPerformed(ActionEvent ae) {
  if(ae.getSource()==jb1){
   if(submitFlag==false){
    try{
     int tempanswer=Integer.parseInt(jtf.getText().trim());
     studentAnswer[count-1]=jtf.getText().trim();
     if(count==1)count=max;
     else count--;
     jlTitle.setText("第"+count+"题");
     jl.setText(question[count-1]);
     jtf.setText("");
     if(studentAnswer[count-1]==null||studentAnswer[count-1].equals("")){
     }else{
      jtf.setText(studentAnswer[count-1]);
     }
    }catch(NumberFormatException nfe){
     JOptionPane.showMessageDialog(this, "请输入正整数!");
     jtf.requestFocus();
    }
   }else{
    if(count==1)count=max;
    else count--;
    jlTitle.setText("第"+count+"题");
    jl.setText(question[count-1]);
    jtf.setEnabled(false);
    jtf.setText(studentAnswer[count-1]);
    if(correct[count-1]==true){
     jlcorrect.setText("√");
    }else{
     jlcorrect.setText("×");
    }
   }
  }
  if(ae.getSource()==jb2){
   if(submitFlag==false){
    try{
     int tempanswer=Integer.parseInt(jtf.getText().trim());
     studentAnswer[count-1]=jtf.getText().trim();
     if(count==max)count=1;
     else count++;
     jlTitle.setText("第"+count+"题");
     jl.setText(question[count-1]);
     jtf.setText("");
     if(studentAnswer[count-1]==null||studentAnswer[count-1].equals("")){
     }else{
      jtf.setText(studentAnswer[count-1]);
     }
    }catch(NumberFormatException nfe){
     JOptionPane.showMessageDialog(this, "请输入正整数!");
     jtf.requestFocus();
    }
   }else{
    if(count==max)count=1;
    else count++;
    jlTitle.setText("第"+count+"题");
    jl.setText(question[count-1]);
    jtf.setEnabled(false);
    jtf.setText(studentAnswer[count-1]);
    if(correct[count-1]==true){
     jlcorrect.setText("√");
    }else{
     jlcorrect.setText("×");
    }
   }
  }
  if(ae.getSource()==jb3){
   try{
    int tempanswer=Integer.parseInt(jtf.getText().trim());
    studentAnswer[count-1]=jtf.getText().trim();
    for(int i=0;i<max;i++){
     if(studentAnswer[i]==null||studentAnswer[i].equals("")){
      correct[i]=false;
     }else if(Integer.parseInt(studentAnswer[i])==answer[i]){
      correct[i]=true;
     }else{
      correct[i]=false;
     }
    }
    int correctAnswer=0;
    for(int i=0;i<max;i++){
     if(correct[i]==true){
      correctAnswer++;
     }
    }
    String s="共"+max+"道题\n";
    s=s+"答对"+correctAnswer+"道题\n";
    s=s+"答错"+(max-correctAnswer)+"道题\n";
    s=s+"成绩"+String.format("%.2f",(100*(float)correctAnswer/max))+"分\n";
    JOptionPane.showMessageDialog(this, s);
    submitFlag=true;
    jtf.setEnabled(false);
    jtf.setText(studentAnswer[count-1]);
    if(correct[count-1]==true){
     jlcorrect.setText("√");
    }else{
     jlcorrect.setText("×");
    }
   }catch(NumberFormatException nfe){
    JOptionPane.showMessageDialog(this, "请输入正整数!");
    jtf.requestFocus();
   }
  }
 }
 public static void main(String[] args) {
  new B();
 }
 private String randomQuestion(int MAX) {
  String s="";
  int answer=MAX+1;
  while(answer>MAX||answer<0){
   int a=(int)(Math.random()*MAX+1);
   int b=(int)(Math.random()*MAX+1);
   int c=(int)(Math.random()*4+1);
   switch(c){
    case 1:answer=a+b;break;
    case 2:answer=a-b;break;
    case 3:answer=a*b;break;
    case 4:
     if(a%b==0){
      answer=a/b;
     }
     break;
   }
   if(answer<=MAX&&answer>=0){
    s=s+a;
    switch(c){
     case 1:s=s+"+";break;
     case 2:s=s+"-";break;
     case 3:s=s+"*";break;
     case 4:s=s+"/";break;
    }
    s=s+b+"="+answer;
   }
  }
  return s;
 }
}

  

四则运算