简单四则运算实现(JAVA)
2021/09/25
因为之前有在JAVA作业做过关于100以内的简单加法的实现,
所以自然想到了套用之前做的作业来进行深化和改写!
先展示原本作业代码,晚点再来进行更新
package luckyLive; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Live_11_1 { public static void main(String[] args) { new NumJframe(); } } class JiafaSum {// 定义全局变量 public static int sum = 0; } class NumJframe extends JFrame { NumJframe() { setLayout(null); Container con = getContentPane();// 创建一个内容面板容器 bank1();// 获得题目 bank2();// 确认答案 setTitle("100以內的加法测试"); setBounds(200, 200, 600, 300);// 设置出现位置和窗口大小 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭方式 setVisible(true);// 设置是否展示窗口 con.setBackground(Color.yellow); } void bank1() {// 获得题目按钮区 JTextField jt = new JTextField();// 文本框1 jt.setBounds(120, 40, 200, 30); JButton jb1 = new JButton();// 按钮1 jb1.setBounds(20, 40, 100, 30); jb1.setText("获得题目"); jb1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int jiashu1 = (int) (Math.random() * 100 + 1); int jiashu2 = (int) (Math.random() * 100 + 1); String js1 = "" + jiashu1; String js2 = "" + jiashu2; JiafaSum.sum = jiashu1 + jiashu2; jt.setText(js1 + "+" + js2 + "=" + ""); } }); add(jt); add(jb1); } void bank2() {// 确认答案按钮区 JTextField jt2 = new JTextField();// 文本框2 jt2.setBounds(120, 75, 200, 30); JButton jb2 = new JButton();// 按钮1 jb2.setBounds(20, 75, 100, 30); jb2.setText("确认答案"); jb2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int sum = JiafaSum.sum; String inputSum = jt2.getText(); int inputsum = Integer.parseInt(inputSum); //System.out.println("sum="+sum); //System.out.println("inputSum="+inputSum); if (inputsum == sum) { JOptionPane.showMessageDialog(null,"答案正确","消息对话框",JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(null,"答案错误","消息对话框",JOptionPane.WARNING_MESSAGE); } } }); add(jt2); add(jb2); } }
2021/09/26
依次为模板,我对该代码进行了更新和小调整,做出了加减乘除的各类
以下是代码:
package Live; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JTextField; import Live.JiafaSum; public class Jia_1 { public static void main(String[] args) { new NumJframe(); } } class JiafaSum {// 定义全局变量(其实就是类变量,因为在JAVA中不存在全局变量的声明,但是类默认是static的, // 所以采用定义类变量的方法) public static int sum = 0; } class NumJframe extends JFrame { NumJframe() { setLayout(null); Container con = getContentPane();// 创建一个内容面板容器 bank1();// 获得题目 bank2();// 确认答案 setTitle("100以內的加法测试"); setBounds(200, 200, 600, 300);// 设置出现位置和窗口大小 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭方式 setVisible(true);// 设置是否展示窗口 con.setBackground(Color.yellow); } void bank1() {// 获得题目按钮区 JTextField jt = new JTextField();// 文本框1 jt.setBounds(120, 40, 200, 30); JButton jb1 = new JButton();// 按钮1 jb1.setBounds(20, 40, 100, 30); jb1.setText("获得题目"); jb1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int jiashu1 = (int) (Math.random() * 100 + 1); int jiashu2 = (int) (Math.random() * 100 + 1); String js1 = "" + jiashu1; String js2 = "" + jiashu2; JiafaSum.sum = jiashu1 + jiashu2; jt.setText(js1 + "+" + js2 + "=" + ""); } }); add(jt); add(jb1); } void bank2() {// 确认答案按钮区 JTextField jt2 = new JTextField();// 文本框2 jt2.setBounds(120, 75, 200, 30); JButton jb2 = new JButton();// 按钮1 jb2.setBounds(20, 75, 100, 30); jb2.setText("确认答案"); jb2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int sum = JiafaSum.sum; String inputSum = jt2.getText(); //System.out.println("sum="+sum); //System.out.println("inputSum="+inputSum); if(inputSum == null) { System.out.println("你的输入有误,请重新输入一个整数:"); } int inputsum = Integer.parseInt(inputSum); if (inputsum == sum) { JOptionPane.showMessageDialog(null,"答案正确","消息对话框",JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(null,"答案错误","消息对话框",JOptionPane.WARNING_MESSAGE); } } }); add(jt2); add(jb2); } }
package Live; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JTextField; public class Jian_1 { public static void main(String[] args) { new NumJframe2(); } } class JianfaSum {// 定义全局变量(其实就是类变量,因为在JAVA中不存在全局变量的声明,但是类默认是static的, // 所以采用定义类变量的方法) public static int sum2 = 0; } class NumJframe2 extends JFrame { NumJframe2() { setLayout(null); Container con = getContentPane();// 创建一个内容面板容器 bank1();// 获得题目 bank2();// 确认答案 setTitle("100以內的减法测试"); setBounds(200, 200, 600, 300);// 设置出现位置和窗口大小 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭方式 setVisible(true);// 设置是否展示窗口 con.setBackground(Color.cyan); } void bank1() {// 获得题目按钮区 JTextField jt = new JTextField();// 文本框1 jt.setBounds(120, 40, 200, 30); JButton jb1 = new JButton();// 按钮1 jb1.setBounds(20, 40, 100, 30); jb1.setText("获得题目"); jb1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int jianshu1 = (int) (Math.random() * 100 + 1); int jianshu2 = (int) (Math.random() * 100 + 1); String js1 = "" + jianshu1; String js2 = "" + jianshu2; JianfaSum.sum2 = jianshu1 - jianshu2; jt.setText(js1 + "-" + js2 + "=" + ""); } }); add(jt); add(jb1); } void bank2() {// 确认答案按钮区 JTextField jt2 = new JTextField();// 文本框2 jt2.setBounds(120, 75, 200, 30); JButton jb2 = new JButton();// 按钮1 jb2.setBounds(20, 75, 100, 30); jb2.setText("确认答案"); jb2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int sum = JianfaSum.sum2; String inputSum = jt2.getText(); // System.out.println("sum="+sum); // System.out.println("inputSum="+inputSum); if (inputSum == null) { System.out.println("你的输入有误,请重新输入一个整数:"); } int inputsum = Integer.parseInt(inputSum); if (inputsum == sum) { JOptionPane.showMessageDialog(null, "答案正确", "消息对话框", JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(null, "答案错误", "消息对话框", JOptionPane.WARNING_MESSAGE); } } }); add(jt2); add(jb2); } }
package Live; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JTextField; public class Cheng_1 { public static void main(String[] args) { new NumJframe3(); } } class ChengfaSum {// 定义全局变量(其实就是类变量,因为在JAVA中不存在全局变量的声明,但是类默认是static的, // 所以采用定义类变量的方法) public static int sum3 = 0; } class NumJframe3 extends JFrame { NumJframe3() { setLayout(null); Container con = getContentPane();// 创建一个内容面板容器 bank1();// 获得题目 bank2();// 确认答案 setTitle("两位数乘两位数的乘法测试"); setBounds(200, 200, 600, 300);// 设置出现位置和窗口大小 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭方式 setVisible(true);// 设置是否展示窗口 con.setBackground(Color.orange); } void bank1() {// 获得题目按钮区 JTextField jt = new JTextField();// 文本框1 jt.setBounds(120, 40, 200, 30); JButton jb1 = new JButton();// 按钮1 jb1.setBounds(20, 40, 100, 30); jb1.setText("获得题目"); jb1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int jiashu1 = (int) (Math.random() * 100 + 1); int jiashu2 = (int) (Math.random() * 100 + 1); String js1 = "" + jiashu1; String js2 = "" + jiashu2; ChengfaSum.sum3 = jiashu1 * jiashu2; jt.setText(js1 + "X" + js2 + "=" + ""); } }); add(jt); add(jb1); } void bank2() {// 确认答案按钮区 JTextField jt2 = new JTextField();// 文本框2 jt2.setBounds(120, 75, 200, 30); JButton jb2 = new JButton();// 按钮1 jb2.setBounds(20, 75, 100, 30); jb2.setText("确认答案"); jb2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int sum = ChengfaSum.sum3; String inputSum = jt2.getText(); //System.out.println("sum="+sum); //System.out.println("inputSum="+inputSum); if(inputSum == null) { System.out.println("你的输入有误,请重新输入一个整数:"); } int inputsum = Integer.parseInt(inputSum); if (inputsum == sum) { JOptionPane.showMessageDialog(null,"答案正确","消息对话框",JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(null,"答案错误","消息对话框",JOptionPane.WARNING_MESSAGE); } } }); add(jt2); add(jb2); } }
package Live; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JTextField; public class Chu_1 { public static void main(String[] args) { new NumJframe4(); } } class ChufaSum {// 定义全局变量(其实就是类变量,因为在JAVA中不存在全局变量的声明,但是类默认是static的, // 所以采用定义类变量的方法) public static int sum4 = 0; } class NumJframe4 extends JFrame { NumJframe4() { setLayout(null); Container con = getContentPane();// 创建一个内容面板容器 bank1();// 获得题目 bank2();// 确认答案 setTitle("100以内的除法测试(结果保留整数部分)"); setBounds(200, 200, 600, 300);// 设置出现位置和窗口大小 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭方式 setVisible(true);// 设置是否展示窗口 con.setBackground(Color.pink); } void bank1() {// 获得题目按钮区 JTextField jt = new JTextField();// 文本框1 jt.setBounds(120, 40, 200, 30); JButton jb1 = new JButton();// 按钮1 jb1.setBounds(20, 40, 100, 30); jb1.setText("获得题目"); jb1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int jiashu1 = (int) (Math.random() * 100 + 1); int jiashu2 = (int) (Math.random() * 100 + 1); String js1 = "" + jiashu1; String js2 = "" + jiashu2; ChufaSum.sum4 = jiashu1 / jiashu2; jt.setText(js1 + "÷" + js2 + "=" + ""); } }); add(jt); add(jb1); } void bank2() {// 确认答案按钮区 JTextField jt2 = new JTextField();// 文本框2 jt2.setBounds(120, 75, 200, 30); JButton jb2 = new JButton();// 按钮1 jb2.setBounds(20, 75, 100, 30); jb2.setText("确认答案"); jb2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int sum = ChufaSum.sum4; String inputSum = jt2.getText(); //System.out.println("sum="+sum); //System.out.println("inputSum="+inputSum); if(inputSum == null) { System.out.println("你的输入有误,请重新输入一个整数:"); } int inputsum = Integer.parseInt(inputSum); if (inputsum == sum) { JOptionPane.showMessageDialog(null,"答案正确","消息对话框",JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(null,"答案错误","消息对话框",JOptionPane.WARNING_MESSAGE); } } }); add(jt2); add(jb2); } }
可以明显看到,这几个代码就是改了个主要要运行的类的名字,以及要用到的全局变量名,虽然显得没啥水平,子类父类啥的也没用到,但是复制粘贴是我现在所能想到的最简单的方法了,如果我某天有能力了,又闲着没事干了,我一定来优化这个代码!
后面的我晚点再努力更新更新,下次一定!