栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

小学生四则运算测试java(UI界面)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

小学生四则运算测试java(UI界面)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ArithmeticTest7 extends Jframe implements ActionListener {

    private final String[] str1 = { "一年级","二年级","三年级","四年级","五年级","六年级"};
    private final String[] str2 = { "开始","换一个","重开"};


    JButton giveAnswer = new JButton("提交");//button for reset
    JButton up[] = new JButton[str2.length];
    JTextField display = new JTextField("");
    JTextField answer = new JTextField("");
    JTextField display2 = new JTextField("余数:(可不填)");
    JTextField answer2 = new JTextField("");
    JTextField user1 = new JTextField("姓名:");
    JTextField user2 = new JTextField("");
    private String name= user2.getText();
    Jtextarea file = new Jtextarea("");

    JScrollPane File=new JScrollPane(file);
    JTextField numberOfDigit1 = new JTextField("年级:");
    JTextField numberOfDigit2 = new JTextField("");
    JComboBox calculate=new JComboBox();
    public Font ziti = new Font("宋体", Font.BOLD, 25);


    public ArithmeticTest7() {

        super("Caltest");
        Font font = new Font("宋体", Font.BOLD, 30);
        JPanel pnlHead = new JPanel(new BorderLayout());
        JPanel pnlHeadUp = new JPanel(new GridLayout(1,5));
        JPanel pnlBodyEast = new JPanel(new GridLayout(2,1));
        JPanel displayArea=new JPanel(new GridLayout(1,5));

        displayArea.add(display);
        display.setFont(font);
        displayArea.add(answer);
        answer.setFont(font);
        displayArea.add(display2);
        display2.setFont(font);

        displayArea.add(answer2);
        answer2.setFont(font);

        pnlHeadUp.add(user1);
        user1.setFont(font);
        pnlHeadUp.add(user2);
        user2.setFont(font);
        name= user2.getText();
        pnlHeadUp.add(numberOfDigit1);
        numberOfDigit1.setFont(font);
 
        pnlBodyEast.add(giveAnswer);
        pnlHeadUp.add(calculate);
        pnlHead.add( displayArea, BorderLayout.CENTER);
        pnlHead.add( pnlHeadUp, BorderLayout.NORTH);

        for (int i = 0; i < str1.length; i++)
        {
            calculate.setFont(font);
            calculate.addItem(str1[i]);
        }


        for (int i = 0; i < str2.length; i++)
        {

            up[i] = new JButton(str2[i]);
            up[i].setFont(font);
            pnlBodyEast.add(up[i]);
        }


        display.setFont(font);
        giveAnswer.setFont(font);
        user1.setEnabled(false);
        display.setEnabled(false);
        display2.setEnabled(false);
        numberOfDigit1.setEnabled(false);

        JPanel pnlBody = new JPanel(new BorderLayout());
        pnlBody.add(File,BorderLayout.CENTER);
        pnlBody.add(pnlBodyEast,BorderLayout.NORTH);

        setLayout(new BorderLayout());
        add(BorderLayout.NORTH, pnlHead);
        add(BorderLayout.CENTER, pnlBody);

        giveAnswer.addActionListener(this);
        display.addActionListener(this);
        calculate.addActionListener(this);

        for (int i = 0; i < str2.length; i++)
        {
            up[i].addActionListener(this);
        }
      
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(800, 500);
        Dimension displaySize = Toolkit.getDefaultToolkit().getScreenSize(); // 获得显示器大小对象
        Dimension frameSize = getSize();
        setLocation((displaySize.width - frameSize.width)/2 , (displaySize.height - frameSize.height)/2 );
        setVisible(true);
    }


    int o=0;
    int score=0;
    int[] Answer=new int[100];
    int[] Answer2=new int[100];
    int[] b=new int[100];
    int[] n=new int[100];
    int[] m=new int[100];
    String[] c=new String[10];
    String N,M;
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        name= user2.getText();

        if (source==up[0])
            CalculateE();

        if (source==up[1])
        {
            CalculateE();
            score=0;
            o=0;
        }
        if (source==up[2])
        {
            CalculateE();

            file.setText("");
            score=0;
            o=0;
        }

        if (source == giveAnswer)
        {

            String q=answer.getText();
            int a=Integer.parseInt(q);
            if(a==Answer[o])
                score=score+10;
            String str = null;

            switch (b[o]) {
                case 1:

                    str=String.format("%-10s%-10s%-10s%-10srn",n[o]+"+"+m[o]+"=",name+"答案"+a,"标准答案:"+Answer[o],"目前得分:"+score);
                    break;
                case 2:
                    if(n[o]=m[o])
                        str=String.format("%-10s%-10s%-10s%-10srn",n[o]+"-"+m[o]+"=",name+"答案"+a,"标准答案:"+Answer[o],"目前得分:"+score);
                    break;
                case 3:
                    str=String.format("%-10s%-10s%-10s%-10srn",n[o]+"*"+m[o]+"=",name+"答案"+a,"标准答案:"+Answer[o],"目前得分:"+score);
                    break;
                case 4:
                    if(n[o]=m[o])
                    {
                        str=String.format("%-10s%-10s%-10s%-10srn",n[o]+"/"+m[o]+"=",name+"答案"+a,"标准答案:"+Answer[o],"目前得分:"+score);
                    }
                    break;
            }

            file.setFont(ziti);
            file.setText(file.getText()+str);

            if(o<10)
            {
                o=o+1;
                if(o!=10)
                {display.setText(c[o]);
                    answer.setText("");
                    answer2.setText("");}
            }

        }
    }

    public void CalculateE()
    {

        if(calculate.getSelectedItem()=="一年级")
        {

            for(int i=0;i<10;i++)
            {
                b[i]=(int)(Math.random()*2)+1;
                n[i]=(int)(Math.random()*(Math.pow(10, 1)))+1;
                m[i]=(int)(Math.random()*(Math.pow(10, 1)))+1;
            }
        }
        if(calculate.getSelectedItem()=="二年级")
        {

            for(int i=0;i<10;i++) {
                b[i] = (int) (Math.random() * 2) + 1;
                n[i] = (int) (Math.random() * (Math.pow(10, 2))) + 1;
                m[i] = (int) (Math.random() * (Math.pow(10, 2))) + 1;
            }
        }
        if(calculate.getSelectedItem()=="三年级")
        {

            for(int i=0;i<10;i++) {
                b[i] = (int) (Math.random() * 2) + 2;
                n[i] = (int) (Math.random() * (Math.pow(10, 2))) + 1;
                m[i] = (int) (Math.random() * (Math.pow(10, 2))) + 1;
            }
        }
        if(calculate.getSelectedItem()=="四年级")
        {

            for(int i=0;i<10;i++) {
                b[i] = (int) (Math.random() * 4) + 1;
                n[i] = (int) (Math.random() * (Math.pow(10, 2))) + 1;
                m[i] = (int) (Math.random() * (Math.pow(10, 2))) + 1;
            }
        }
        if(calculate.getSelectedItem()=="五年级")
        {

            for(int i=0;i<10;i++){
                b[i]=(int)(Math.random()*4)+1;
                n[i]=(int)(Math.random()*(Math.pow(10, 3)))+1;
                m[i]=(int)(Math.random()*(Math.pow(10, 3)))+1;
            }
        }
        if(calculate.getSelectedItem()=="六年级")
        {

            for(int i=0;i<10;i++) {
                b[i] = (int) (Math.random() * 4) + 1;
                n[i] = (int) (Math.random() * (Math.pow(10, 3))) + 1;
                m[i] = (int) (Math.random() * (Math.pow(10, 3))) + 1;
            }
        }


        for(int i=0;i<10;i++)
        {
            N=String.valueOf(n[i]);
            M=String.valueOf(m[i]);
            switch (b[i]) {
                case 1:
                    c[i]=N+"+"+M+"=";
                    Answer[i]=n[i]+m[i];
                    break;
                case 2:
                    if(n[i]=m[i])
                        c[i]=N+"-"+M+"=";
                    Answer[i]=Math.abs(m[i]-n[i]);
                    break;
                case 3:
                    Answer[i]=n[i]*m[i];
                    c[i]=N+"*"+M+"=";
                    break;
                case 4:
                    if(n[i]=m[i])
                    {
                        c[i]=N+"/"+M+"=";
                        Answer[i]=n[i]/m[i];
                        Answer2[i]=n[i]%m[i];
                    }
                    break;
            }
        }
        display.setText(c[0]);
    }



    public static void main(String[] args) {

        ArithmeticTest7 test = new ArithmeticTest7();

    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/434371.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号