Java程序实现电子算盘

发布于 2022-09-01 21:36:31 字数 438 浏览 33 评论 0

给小朋友设计一个珠算测试器,要求能够完成珠算加减法的测试。具体的要求功能如下:
(1) 用户启动测试,输入用户名后系统随机生成特定数目的加减法测试题;
(2) 要求测试使用表盘式或数字时秒表进行界面计时显示(参考如上图示);
(3) 对于每道测试题目,要求用户使用电子算盘完成珠算过程,当按下确认键时,将珠
算结果与正确答案比对,并在界面上显示总题数、已答题数和已做对题数;
(4) 当测试完成,界面显示本次测试情况(包括用户名、测试题目及答题明细、对错情
况、测试用时和测试成绩)
clipboard.png
并要实现这个界面。

大神们,在线急等,我是真不会啊

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲影 2022-09-08 21:36:31
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Container;

import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JTextField;

import java.awt.event.*;
import java.lang.*;
import java.util.Timer;
import java.util.TimerTask;


public class Suanpan extends JFrame
{
    public static void main(String[]args)
    {
        Suanpan f = new Suanpan();
    }
    public int totalNumber;    
    public int answer;
    public boolean isAnswer = true;
    public int t = 0;
    public int answeredQuestion = 0;
    public int rightA_Number = 0;
    public int question_Number;
    public boolean[] isAddFive = new boolean[4];
    public int number[] = new int[4];
    public ImageIcon picture = new ImageIcon(System.getProperty("user.dir") + "/算珠.jpg" );

    Container con = getContentPane();
    JPanel j1 = new JPanel();
    JPanel j2 = new JPanel();
    JPanel j3 = new JPanel();
    JLabel q = new JLabel();
    JLabel nA = new JLabel();
    JLabel nB = new JLabel();    
    JLabel n = new JLabel();
    JLabel b = new JLabel();
    JLabel s = new JLabel();
    JLabel sN = new JLabel();
    JLabel tM = new JLabel();
    JTextField userName = new JTextField();
    JTextField sNumber = new JTextField();
    JButton beginAnswer = new JButton("开始答题");
    JButton oK = new JButton("确定");
    JButton nQ = new JButton("下一题");
    JTextField a = new JTextField();
    JButton labels_1[][] = new JButton[2][4];
    JButton labels_2[][] = new JButton[5][4];
    GridLayout gl_1 = new GridLayout(2,4);
    GridLayout gl_2 = new GridLayout(5,4);
    public Suanpan()
    {
    super("模拟算盘");    
    Init();    
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
    setBounds(100,100,600,500);    
    setVisible(true);    
    setLayout(null);        
    AbacusPanel();    
    DrawMnue();
    
    }
    Timer timer;
    // 计时器
    public void QuestionTimer()
    {
    timer = new Timer();
    timer.schedule(new RunTask(),1000,1000);
    }
    class RunTask extends TimerTask
    {
    public void run()
    {
        t = t + 1;
        String ss = "";
        if(t / 60 < 10)    
        ss = ss + "0";    
        ss = ss + Integer.toString(t / 60) + ":";    
        if(t % 60 < 10)        
        ss = ss + "0";    
        ss = ss + Integer.toString(t % 60);    
        tM.setText("计时:   " + ss);    
    }
    }
   
    
    
    // 算盘面板
    public void AbacusPanel() 
    {    
    j1.setLayout(null);        
    j1.setBounds(15,160,400,300);
    j1.setBackground(Color.black);    
    con.add(j1);    
    JPanel j2 = new JPanel();    
    j2.setLayout(gl_1);
    j2.setBounds(10, 10, 380, 80);
    j1.add(j2);
    JPanel j3 = new JPanel();    
    j3.setLayout(gl_2);    
    j3.setBounds(10, 100, 380, 190);
    j1.add(j3);        
    for(int i = 0;i < 2;i++)
    {    
        for(int j = 0;j < 4;j++)
        {
        labels_1[i][j] = new JButton("");
        labels_1[i][j].addActionListener(new ClickIt());
        labels_1[i][j].setBorderPainted(false);
        labels_1[i][j].setContentAreaFilled(false);
        if(i != 1)
            labels_1[i][j].setIcon(picture);
        j2.add(labels_1[i][j]);
        }    
    }
    for(int i = 0;i < 5;i++)
    {        
        for(int j = 0;j < 4;j++)
        {
        labels_2[i][j] = new JButton("");
        labels_2[i][j].addActionListener(new ClickIt());
        labels_2[i][j].setBorderPainted(false);
        labels_2[i][j].setContentAreaFilled(false);
        if(i != 0)
            labels_2[i][j].setIcon(picture);
        j3.add(labels_2[i][j]);    
        }
    }
    }
    //编辑选项
    public void DrawMnue()
    {
    Font f1 = new Font("",Font.BOLD,30);
    Font f2 = new Font("",Font.BOLD,14);
    Font f3 = new Font("",Font.PLAIN,18);
        
    q.setBounds(20, 50, 280, 40);
    q.setFont(f1);
    con.add(q);
        
    a.setFont(f1);
    a.setBounds(270,50,80,40);
    a.setVisible(false);
    con.add(a);
        
    oK.setFont(f2);    
    oK.setBounds(350, 50, 65, 40);
    oK.addActionListener(new ClickOk());
    oK.setVisible(false);
    con.add(oK);
        
    nA.setText("");
    nA.setBounds(20,100,300,40);
    nA.setFont(f1);
    con.add(nA);
        
    nQ.setFont(f2);
    nQ.setBounds(420, 50, 80, 40);
    nQ.addActionListener(new ClickNext());
    nQ.setVisible(false);
    con.add(nQ);
        
    nB.setText("已做对题数:" + rightA_Number);
    nB.setFont(f3);
    nB.setForeground(Color.red);
    nB.setBounds(430, 150, 140, 40);
    con.add(nB);
        
    n.setText("已答题数:" + answeredQuestion);
    n.setFont(f3);
    n.setForeground(Color.red);
    n.setBounds(430, 200, 140, 40);
    con.add(n);
        
    sN.setText("总题数:" + question_Number);
    sN.setFont(f3);
    sN.setBounds(430, 250, 140, 40);
    con.add(sN);
        
    b.setText("用户名:");
    b.setFont(f3);
    b.setBounds(20, 0, 80, 40);
    con.add(b);

    userName.setText("");
    userName.setFont(f3);
    userName.setBounds(100, 10, 60, 30);
    userName.setHorizontalAlignment(0);
    con.add(userName);
        
    s.setText("设置题目总数:");
    s.setFont(f3);
    s.setBounds(180,0,130,40);
    con.add(s);
        
    sNumber.setText("");
    sNumber.setFont(f3);
    sNumber.setBounds(300, 10, 60, 30);
    sNumber.setHorizontalAlignment(0);
    con.add(sNumber);
        
    beginAnswer.setFont(new Font("",Font.PLAIN,12));
    beginAnswer.setBounds(400, 5, 85,30);
    beginAnswer.addActionListener(new ClickLogin());
    con.add(beginAnswer);
        
    tM.setText("计时:   00:00");
    tM.setFont(f3);
    tM.setBounds(430, 300, 200, 40);
    con.add(tM);
    }
    
    
    // 生成题目
    private void Question()
    {    
    int i1 = 1 + (int)(Math.random()*9999);
    int i2 = 1 + (int)(Math.random()*9999);
    int number1 = Math.max(i1, i2);
    int number2 = Math.min(i1, i2);
    int m = (int)(Math.random()*2);
    if(m == 0)
    {
        while(i1 + i2 > 10000)
        {
            i1 = 1 + (int)(Math.random()*9999);
            i2 = 1 + (int)(Math.random()*9999);
        }    
        q.setText(Integer.toString(i1) + "   +   " + Integer.toString(i2) + "   =   ");
        answer = i1 + i2;
    }
    else
    {
        q.setText(Integer.toString(number1) + "   -   " + Integer.toString(number2) + "   =   ");
        answer = number1 - number2;    
    }
    }
    
    // 计算算盘的结果
    private void addTotalNumber()
    {    
    for(int j = 0;j < 4;j++)
    {
        if(isAddFive[j] == true && number[j] < 5)
        number[j] = number[j] + 5;        
        else 
        if(isAddFive[j] == false && number[j] >= 5)
            number[j] = number[j] - 5;
    }    
    totalNumber = 0;
    for(int j = 0;j < 4;j++)
    {
        totalNumber = totalNumber + number[j] * (int)Math.pow(10,(double)j);
    }    
    System.out.println(totalNumber);
    }
    
    // 初始化数据
    private void Init()
    {
    totalNumber = 0;
    for(int j = 0;j < 4;j++)
    {
        number[j] = 0;
        isAddFive[j] = false;    
    }
    }
    
    // 绘制j列算珠
    private void DrawCol(int j)
    {
    for(int i = 1; i <=4;i++)
    {
        labels_2[i][j].setIcon(picture);
    }
    }
    
    // 单击事件监听器
    class ClickIt implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
        // TODO 自动生成的方法存根
        if(isAnswer == false)
        {
        for(int i = 0;i < 2;i++)
            for(int j = 0;j < 4;j++)    
            if(e.getSource() == labels_1[i][j])
            {
                if(i == 1)    
                {
                isAddFive[4-j-1] = false;
                labels_1[0][j].setIcon(picture);
                labels_1[1][j].setIcon(null);    
                }    
                else 
                if(i == 0)
                {
                    isAddFive[4-j-1] = true;
                    labels_1[1][j].setIcon(picture);
                    labels_1[0][j].setIcon(null);    
                }
            }
        // 点击后动作
        for(int i = 0;i < 5;i++)
            for(int j = 0;j < 4;j++)
            if(e.getSource() == labels_2[i][j])
            {
                number[4-j-1] = i;        
                DrawCol(j);
                labels_2[0][j].setIcon(picture);
                labels_2[i][j].setIcon(null);
            }
        addTotalNumber();
        }
    }
    }

    class ClickOk implements ActionListener
    {
    public void actionPerformed(ActionEvent e) 
    {
        isAnswer = true;
        timer.cancel();    
        a.setText(Integer.toString(totalNumber));
        if(totalNumber == answer)
        {
        oK.setText("正确");
        oK.setForeground(Color.green);
        rightA_Number = rightA_Number + 1;
        }
        else
        {
        oK.setText("错误");
        oK.setForeground(Color.red);
        nA.setText("正确答案为 :  " + Integer.toString(answer));    
        nA.setForeground(Color.green);
        }
        answeredQuestion = answeredQuestion + 1;
        nB.setText("已做对题数:" + rightA_Number);
        nB.setForeground(Color.red);
        n.setText("已答题数:" + answeredQuestion);    
        n.setForeground(Color.red);
    }    
    }
    class ClickNext implements ActionListener
    {
    public void actionPerformed(ActionEvent e) 
    {
        if(isAnswer == true)
        {    
        isAnswer = false;
        if(nQ.getText() == "完成")
        {    
            timer.cancel();
            double x = ((double)rightA_Number / (double)question_Number) * 100;
            int option = JOptionPane.showConfirmDialog(null , "您的正确率为: " + x + "%\n用时" + t + "秒"+"\n是否重新开始?" , "提示", JOptionPane.YES_NO_OPTION );
            if(JOptionPane.YES_OPTION == option)
            {                     
            userName.setText("");
            userName.setEditable(true);        
            sNumber.setText("");        
            sNumber.setEditable(true);
            beginAnswer.setEnabled(true);
            nQ.setVisible(false);            
            oK.setVisible(false);    
            a.setVisible(false);            
            q.setVisible(false);
            oK.setText("确定");
            oK.setForeground(null);
            a.setText("");
            nA.setText("");                
            nQ.setText("下一题");
            Init();                        
            rightA_Number = 0;
            answeredQuestion = 0;
            t = 0;
            n.setText("已答题数:" + answeredQuestion);
            n.setForeground(Color.red);
            nB.setText("已做对题数:" + rightA_Number);
            nB.setForeground(Color.red);
            sN.setText("总题数:" + 0);
            sN.setForeground(Color.red);
            tM.setText("计时:" + 0);
            }    
        }    
        else
        {
            QuestionTimer();
            Question();
            oK.setText("确定");
            oK.setForeground(null);
            a.setText("");
            nA.setText("");        
            // 初始化算盘
            for(int i = 0;i < 4;i++)
            {
            DrawCol(i);
                labels_1[1][i].setIcon(null);
                labels_2[0][i].setIcon(null);
                 labels_1[0][i].setIcon(picture); 
            }
            Init();          
            if(answeredQuestion == question_Number - 1)    
            nQ.setText("完成");        
        }
        }    
    }
    }
    class ClickLogin implements ActionListener
    {
    public void actionPerformed(ActionEvent e) 
    {

        boolean isInt = true;
        for(int i = 0;i < sNumber.getText().length();i++)        
        if(!Character.isDigit(sNumber.getText().charAt(i)))
            isInt = false;
        if(userName.getText().equalsIgnoreCase(""))
        JOptionPane.showMessageDialog(null, "请输入用户名 !");    
        else 
        if(sNumber.getText().equalsIgnoreCase("") || isInt == false)    
            JOptionPane.showMessageDialog(null, "请输入题目总数 !");
        else
        {    
            isAnswer = false;
            Question();
            beginAnswer.setEnabled(false);
            q.setVisible(true);    
            nQ.setVisible(true);    
            oK.setVisible(true);    
            a.setVisible(true);            
            userName.setEditable(false);
            sNumber.setEditable(false);
            String xx = sNumber.getText();
            question_Number = Integer.parseInt(xx);    
            sN.setText("总题数:" + question_Number);    
            QuestionTimer();    
        }
    }    
    }    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文