口算系统

口算系统

口算系统

口算系统

代码:

1.package mv;
import java.util.Random;
public abstract class Equation {
static final int UPPER=100;
static final int LOWWER=0;
private int intAnswer;
private int LeftOpNum;
private int RightOpNum;
private char oper;
public int getIntAnswer() {
return intAnswer;
}
public int getLeftOpNum() {
return LeftOpNum;
}
public int getRightOpNum() {
return RightOpNum;
}
public char getOper() {
return oper;
}
public void generateBinaryOperation(char anOperator){
int left,right,result;
Random random=new Random();
left=random.nextInt(UPPER);
do{
right=random.nextInt(UPPER);
result=calculate(left,right);
}while(!(checkingCalculation(result)));
LeftOpNum=left;
RightOpNum=right;
oper=anOperator;
intAnswer=result;
}
public abstract boolean checkingCalculation(int anInteger);
public abstract int calculate(int left,int right);
// 直接打印算式字符串
public String toString() {
String s = "" + getLeftOpNum() + getOper() + getRightOpNum() + "=";
return s;
}
}
class AddOperation extends Equation{
AddOperation(){
generateBinaryOperation('+');
}
public boolean checkingCalculation(int anInteger){
return anInteger<=UPPER;
}
@Override
public int calculate(int left, int right) {
// TODO Auto-generated method stub
return left+right;
}
}
class SubOperation extends Equation{
SubOperation(){
generateBinaryOperation('-');
}
@Override
public boolean checkingCalculation(int anInteger) {
// TODO Auto-generated method stub
return anInteger>=LOWWER;
}
@Override
public int calculate(int left, int right) {
// TODO Auto-generated method stub
return left-right;
}

}

2.package mv;
import java.util.Random;
class Exercise {
Equation List[]=new Equation[200];
// 选择生成习题类型及数量
public void generateBinaryExercise(int operationCount, int opValue) {
Equation anOperation;
do {
anOperation = gerOperation(opValue);
} while (contains(anOperation));
List[operationCount] = anOperation;
}


//选择加法或减法
public Equation gerOperation(int opValue) {
if (opValue == 1) {
return new AddOperation();
}else
return new SubOperation();
}
//生成加法习题
public void generateAdditionOperation(int operationCount) {
for (int i = 0; i < operationCount; i++) {
generateBinaryExercise(i,1);
}
}
//生成减法习题
public void generateSubstractOperation(int operationCount) {
for (int i = 0; i < operationCount; i++) {
generateBinaryExercise(i,0);
}
}


//生成加法和减法混合习题
public void generateAddAndSubOperation(int operationCount) {
Random random = new Random();
for (int i = 0; i < operationCount; i++) {
int r;
r = random.nextInt(2);
if (r == 1) {
generateBinaryExercise(i,1);
} else
generateBinaryExercise(i,0);
}
}
//比较重复
private boolean contains(Equation anOperation){
boolean found=false;
for(int i=0;i<List.length;i++){
if(anOperation.equals(List[i])){
found=true;
break;
}
}
return found;
}

}

3.

package mv;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Frame;


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Scanner;
import java.awt.event.ActionEvent;
import java.awt.SystemColor;
import javax.swing.UIManager;
import javax.swing.JPasswordField;


public class Login extends JFrame {


private JPanel contentPane;
private Frame myLabel;
private JTextField textField;
private JTextField textField_1;
private JPasswordField passwordField;


/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login frame = new Login();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}


/**
* Create the frame.
*/
public Login() {
setTitle("登录界面");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBackground(Color.CYAN);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);


JPanel jp1 = (JPanel) this.getContentPane();
contentPane.setLayout(null);
contentPane.setLayout(null);


JLabel label = new JLabel("\u8D26\u53F7\uFF1A");
label.setBounds(88, 81, 72, 18);
contentPane.add(label);


JLabel label_1 = new JLabel("\u5BC6\u7801\uFF1A");
label_1.setBounds(88, 141, 72, 18);
contentPane.add(label_1);


textField = new JTextField();
textField.setBounds(149, 78, 126, 24);
contentPane.add(textField);
textField.setColumns(10);


passwordField = new JPasswordField();
passwordField.setBounds(149, 138, 126, 24);
contentPane.add(passwordField);
passwordField.setColumns(10);


JButton btnNewButton = new JButton("登录");
btnNewButton.setBackground(UIManager.getColor("textHighlight"));
btnNewButton.setForeground(SystemColor.textHighlight);
btnNewButton.addActionListener(new ActionListener() {
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
if(textField.getText().equals("hsy")&&passwordField.getText().equals("201519812")){
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PracticeGui frame = new PracticeGui();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
dispose();
}
}
});
btnNewButton.setBounds(162, 189, 113, 27);
contentPane.add(btnNewButton);


}

}

4.

package mv;


import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.SystemColor;


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;


import javax.swing.JButton;
import javax.swing.JTextArea;


import java.awt.event.ActionListener;
import java.util.Random;
import java.awt.event.ActionEvent;
public class PracticeGui extends JFrame {
static final int WIN_WIDTH = 580;
static final int WIN_HEIGHTH = 500;
static final int OP_COUNT = 20;
static final int OP_COLUMN = 5;
static final int OP_WIDTH = 65;
static final int ANSWER_WIDTH = 35;
static final int COMPONET_HEIGHT = 25;
Equation List[]=new Equation[200];


private JPanel contentPane;
private JTextField [] tfOp;
private JTextField [] tfAns;
private JTextArea taStat;


private Exercise exercises;
private int correctAmount;
private int wrongAmount;
private float correctRatio;
private float wrongRatio;


// public static void main(String[] args) {
// EventQueue.invokeLater(new Runnable() {
// public void run() {
// try {
// PracticeGui frame = new PracticeGui();
// frame.setVisible(true);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// });
// }
private void initExerciseComponets(){
exercises = new Exercise();
exercises.generateAdditionOperation(OP_COUNT);


tfOp = new JTextField[OP_COUNT];
tfAns = new JTextField[OP_COUNT];
for(int i=0; i<OP_COUNT; i++){
tfOp[i] = new JTextField();
tfOp[i].setBounds(20 + (i%OP_COLUMN)*(OP_WIDTH+ANSWER_WIDTH+5),
20 + (i/OP_COLUMN)*(COMPONET_HEIGHT+10),
OP_WIDTH,
COMPONET_HEIGHT);
tfOp[i].setHorizontalAlignment(JTextField.RIGHT);
tfOp[i].setEditable(false);
contentPane.add(tfOp[i]);


tfAns[i] = new JTextField();
tfAns[i].setBounds(20+OP_WIDTH+(i%OP_COLUMN)*(OP_WIDTH+ANSWER_WIDTH+5),
20+(i/OP_COLUMN)*(COMPONET_HEIGHT+10),
ANSWER_WIDTH,
COMPONET_HEIGHT);
contentPane.add(tfAns[i]);
}
}
public void updateExerciseComponets(){                   //混合算式


Random random = new Random();
for(int i=0; i<OP_COUNT; i++){
int r;
r = random.nextInt(2);
List[i] = exercises.gerOperation(r);


tfOp[i].setText(List[i].toString());
tfAns[i].setBackground(Color.WHITE);
tfAns[i].setText("");
}
taStat.setText("统计信息:\n总题数:"+OP_COUNT+"\n正确题数:\t错误题数:\n正确率:\t\t错误率:");
}


public void updateExerciseComponets1(){                   //加法算式


for(int i=0; i<OP_COUNT; i++){
List[i] = exercises.gerOperation(1);


tfOp[i].setText(List[i].toString());
tfAns[i].setBackground(Color.WHITE);
tfAns[i].setText("");
}
taStat.setText("统计信息:\n总题数:"+OP_COUNT+"\n正确题数:\t错误题数:\n正确率:\t\t错误率:");
}


public void updateExerciseComponets2(){                   //减法算式


for(int i=0; i<OP_COUNT; i++){
List[i] = exercises.gerOperation(0);


tfOp[i].setText(List[i].toString());
tfAns[i].setBackground(Color.WHITE);
tfAns[i].setText("");
}
taStat.setText("统计信息:\n总题数:"+OP_COUNT+"\n正确题数:\t错误题数:\n正确率:\t\t错误率:");
}
private void judge(){


correctAmount = wrongAmount = 0;
for(int i=0; i<OP_COUNT; i++){


String result = String.valueOf(List[i].getIntAnswer());
String answer = tfAns[i].getText().trim();
if(result.equals(answer)){
tfAns[i].setBackground(Color.GREEN);
correctAmount++;
}else{
tfAns[i].setBackground(Color.RED);
wrongAmount++;
}
}
correctRatio = (float)correctAmount / OP_COUNT;
wrongRatio = wrongAmount / OP_COUNT;
taStat.setText("统计信息:\n总题数:" + OP_COUNT 
+ "\n正确题数:" + correctAmount
+ "\t错误题数:" + wrongAmount
+ "\n正确率:" + String.format("%.2f", 100*correctRatio)+"%"
+ "\t错误率:" + String.format("%.2f", 100*wrongRatio)+"%");
}
public PracticeGui() {
setTitle("口算练习");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, WIN_WIDTH, WIN_HEIGHTH);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setBackground(Color.CYAN);
setContentPane(contentPane);
contentPane.setLayout(null);


JButton btnGenrate = new JButton("重新产生混合算式");              //生成混合算式按钮
btnGenrate.setBackground(UIManager.getColor("textHighlight"));
btnGenrate.setForeground(SystemColor.textHighlight);
btnGenrate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {


updateExerciseComponets();
}
});
btnGenrate.setBounds(32, 168, 150, 23);
contentPane.add(btnGenrate);






JButton btnGenrate1 = new JButton("重新产生加法算式");            //生成加法算式按钮
btnGenrate1.setBackground(UIManager.getColor("textHighlight"));
btnGenrate1.setForeground(SystemColor.textHighlight);
btnGenrate1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {


updateExerciseComponets1();
}
});
btnGenrate1.setBounds(210, 168, 150, 23);
contentPane.add(btnGenrate1);                      




JButton btnGenrate2 = new JButton("重新产生减法算式");            //生成减法算式按钮
btnGenrate2.setBackground(UIManager.getColor("textHighlight"));
btnGenrate2.setForeground(SystemColor.textHighlight);
btnGenrate2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {


updateExerciseComponets2();
}
});
btnGenrate2.setBounds(390, 168, 150, 23);
contentPane.add(btnGenrate2);                      




JButton btnSubmit = new JButton("提交答案");
btnSubmit.setBackground(UIManager.getColor("textHighlight"));
btnSubmit.setForeground(SystemColor.textHighlight);
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
judge();
}
});
btnSubmit.setBounds(32, 208, 123, 23);
contentPane.add(btnSubmit);


taStat = new JTextArea();
taStat.setEditable(false);
taStat.setBounds(200, 350, 350, 100);
taStat.setBackground(new Color(255, 255, 0));
taStat.setFont(new Font("宋体", Font.PLAIN, 16));
taStat.setText("统计信息:\n总题数:"+OP_COUNT+"\n正确题数:\t错误题数:\n正确率:\t\t错误率:");
contentPane.add(taStat);


initExerciseComponets();
updateExerciseComponets();




JButton btnNewButton = new JButton("退出");                  //退出按钮
btnNewButton.setBackground(UIManager.getColor("textHighlight"));
btnNewButton.setForeground(SystemColor.textHighlight);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Login frame = new Login();
frame.setVisible(true);
dispose();



});
btnNewButton.setBounds(32, 400, 123, 23);
contentPane.add(btnNewButton);
}
}