口算系统



代码:

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);
}
}

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值