四则运算
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | import javax.swing.*; import java.awt.*; import java.awt.event.*; //设置界面框架 public class text1_1 { public static void main(String[] args) { Win win = new Win(); win.setTitle( "四则运算" ); win.setBounds( 0 , 0 , 500 , 200 ); } } //设置界面 class Win extends JFrame{ Win(){ setLayout( null ); Container con = getContentPane(); //设置底层容器 con.setBackground(Color.blue); init(); setBounds( 10 , 10 , 460 , 360 ); setVisible( true ); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void init(){ JButton button1 = new JButton( "加法题目" ); JTextArea textshow1 = new JTextArea(); JButton button2 = new JButton( "减法题目" ); JTextArea textshow2 = new JTextArea(); JButton button3 = new JButton( "乘法题目" ); JTextArea textshow3 = new JTextArea(); JButton button4 = new JButton( "除法题目" ); JTextArea textshow4 = new JTextArea(); JButton button6 = new JButton( "重置" ); textshow1.setFont( new Font( "楷体" ,Font.BOLD, 20 )); //修改字体样式 textshow2.setFont( new Font( "楷体" ,Font.BOLD, 20 )); textshow3.setFont( new Font( "楷体" ,Font.BOLD, 20 )); textshow4.setFont( new Font( "楷体" ,Font.BOLD, 20 )); Listen listen = new Listen(); listen.setJTextArea1(textshow1); listen.setJTextArea2(textshow2); listen.setJTextArea3(textshow3); listen.setJTextArea4(textshow4); button1.addActionListener(listen); button2.addActionListener(listen); button3.addActionListener(listen); button4.addActionListener(listen); button6.addActionListener(listen); button1.setBounds( 10 , 10 , 100 , 25 ); textshow1.setBounds( 110 , 10 , 200 , 25 ); button2.setBounds( 10 , 40 , 100 , 25 ); textshow2.setBounds( 110 , 40 , 200 , 25 ); button3.setBounds( 10 , 70 , 100 , 25 ); textshow3.setBounds( 110 , 70 , 200 , 25 ); button4.setBounds( 10 , 100 , 100 , 25 ); textshow4.setBounds( 110 , 100 , 200 , 25 ); button6.setBounds( 350 , 50 , 100 , 25 ); add(button1); add(textshow1); add(button2); add(textshow2); add(button3); add(textshow3); add(button4); add(textshow4); add(button6); } } //设置监视器 class Listen implements ActionListener{ JTextArea textshow1; JTextArea textshow2; JTextArea textshow3; JTextArea textshow4; int numberOne; int numberTwo; public void setJTextArea1(JTextArea textshow1) { this .textshow1 = textshow1; } public void setJTextArea2(JTextArea textshow2) { this .textshow2 = textshow2; } public void setJTextArea3(JTextArea textshow3) { this .textshow3 = textshow3; } public void setJTextArea4(JTextArea textshow4) { this .textshow4 = textshow4; } public void setnumberOne( int numberOne) { this .numberOne = numberOne; } public void setnumberTwo( int numberTwo) { this .numberTwo = numberTwo; } public void actionPerformed(ActionEvent e) { String str = e.getActionCommand(); if (str.equals( "加法题目" )) { numberOne = ( int )(Math.random()* 100 ); numberTwo = ( int )(Math.random()* 100 ); setnumberOne(numberOne); setnumberTwo(numberTwo); int sum1 = numberOne + numberTwo; textshow1.append(numberOne+ "+" +numberTwo+ "=" +sum1); } if (str.equals( "减法题目" )) { numberOne = ( int )(Math.random()* 100 ); numberTwo = ( int )(Math.random()* 100 ); setnumberOne(numberOne); setnumberTwo(numberTwo); int sum2 = numberOne - numberTwo; textshow2.append(numberOne+ "-" +numberTwo+ "=" +sum2); } if (str.equals( "乘法题目" )) { numberOne = ( int )(Math.random()* 100 ); numberTwo = ( int )(Math.random()* 100 ); setnumberOne(numberOne); setnumberTwo(numberTwo); int sum3 = numberOne * numberTwo; textshow3.append(numberOne+ "*" +numberTwo+ "=" +sum3); } if (str.equals( "除法题目" )) { numberOne = ( int )(Math.random()* 100 ); numberTwo = ( int )(Math.random()* 100 ); setnumberOne(numberOne); setnumberTwo(numberTwo); float sum4 =( float ) numberOne / numberTwo; textshow4.append(numberOne+ "/" +numberTwo+ "=" +sum4); } if (str.equals( "重置" )){ textshow1.setText( "" ); textshow2.setText( "" ); textshow3.setText( "" ); textshow4.setText( "" ); } } } |
以下是代码运行界面:
其中界面颜色、按钮参数等可在代码中修改;通过点击加法/减法/乘法/除法题目,可以实现自动出题,点击重置则可以清除已有题目。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?