Java简单四则运算练习器

发布时间:2022-07-05 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Java简单四则运算练习器脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
package DAO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class E12 {
    public static void main(String[] args) {
        WindowLogin1 win = new WindowLOGin1("四则运算练习器");    
    }

}
class WindowLogin1 extends JFrame implements ActionListener{
        JButton name,name2;
        JTextField text1,text2;
        int a,b,fuhao;
    WindowLogin1(String s){
        suPEr(s);
          addWindowListener( new WindowAdapter(){
                                public void windowClosing(WindowEvent e) {
                                    dispose();
                                }
                             } 
                           );
        setLayout(null);
        Container con=getContentPane();
        con.setBackground(Color.yellow);
        setBounds(0,0,450,350);
        setDefaultCloseoperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        name2=new JButton("确认答案");
        name=new JButton("获得题目");
        text1=new JTextField();
        text2=new JTextField();
        name.setBounds(10,20,100,20);
        name2.setBounds(10,50,100,20);
        text1.setBounds(120,20,160,20);
        text2.setBounds(120,50,160,20);
        add(name);
        add(name2);
        add(text1);
        add(text2);
        name2.addActionListener(this);
        name.addActionListener(this);
        setVisible(true);
        validate();
        }
    
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==name) {
           a=(int)(Math.random()*100)+1;
           b=(int)(Math.random()*100)+1;
           fuhao=(int)(Math.random()*4)+1;
           if(fuhao==1)
           {text1.setText(a+"+"+b);}
           if(fuhao==2)
            {text1.setText(a+"-"+b);}
           if(fuhao==3)
           {text1.setText(a+"×"+b);}
           if(fuhao==4)
           {text1.setText(a+"÷"+b);}
        }
        else if(e.getSource()==name2) {
           int guess=0;
           guess=Integer.parseint(text2.getText());
           if(guess==a+b&&fuhao==1) {
            JOptionPane.showMessageDialog(this,"计算正确","消息对话框",JOptionPane.INFORMATION_MESSAGE);       
                  }
           else if(guess==a-b&&fuhao==2) {
            JOptionPane.showMessageDialog(this,"计算正确","消息对话框",JOptionPane.INFORMATION_MESSAGE);
           }
           else if(guess==a*b&&fuhao==3) {
               JOptionPane.showMessageDialog(this,"计算正确","消息对话框",JOptionPane.INFORMATION_MESSAGE);
           }
           else if(guess==a/b&&fuhao==4) {
               JOptionPane.showMessageDialog(this,"计算正确","消息对话框",JOptionPane.INFORMATION_MESSAGE);
           }
           else{
               JOptionPane.showMessageDialog(this,"计算错误","消息对话框",JOptionPane.WARNING_MESSAGE);
               text2.setText(null);
           }
                  }
           }
        
    }
    
    

Java简单四则运算练习器

 

Java简单四则运算练习器

 

脚本宝典总结

以上是脚本宝典为你收集整理的Java简单四则运算练习器全部内容,希望文章能够帮你解决Java简单四则运算练习器所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。