首页 > 代码库 > 结对编程:简易四则运算
结对编程:简易四则运算
结对编程:简易四则运算
功能:进行简易的四则运算,并根据给出的结果判断正误。
实现:使用java的图形化界面实现。
package six;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
class Num{
private int a;
public Num()
{
this.a = (int)(Math.random()*100);
}
public int get_num()
{
return this.a;
}
public int num_sum(Num B)
{
return this.a + B.a;
}
public int num_dec(Num B)
{
return this.a - B.a;
}
public int num_c(Num B)
{
return this.a * B.a;
}
public int num_chu(Num B)
{
return this.a / B.a;
}
public void flash()
{
this.a = (int)(Math.random()*100);
}
}
public class Main {
private static JFrame frame = new JFrame("简单四则运算");
private static JLabel label1 = new JLabel(" ");
private static JLabel label2 = new JLabel(" ");
private static JLabel label3 = new JLabel(" ");
private static JLabel label4 = new JLabel(" "+"="+" ");
private static JLabel label5 = new JLabel("结果");
private static JButton button = new JButton("查看");
private static JTextField text = new JTextField(5);
private static JPanel pane = new JPanel(); //容器
public static void main(String[] args) {
pane.setBorder(BorderFactory.createEmptyBorder(20,60,20,60));
pane.setLayout(new GridLayout(0,5)); //设置布局
pane.add(label1);
pane.add(label2);
pane.add(label3);
pane.add(label4);
pane.add(text);
pane.add(label5);
pane.add(button);
Num a = new Num(),b = new Num();
//int a = (int)(Math.random()*100);
//int b = (int)(Math.random()*100);
int c = (int)(Math.random()*4)+1;
String p = " ";
final int n;
int q = 0;
if(c == 1)
{
p = "+";
q = a.num_sum(b);
}
if(c == 2)
{
p = "-";
q = a.num_dec(b);
}
if(c == 3)
{
p = "*";
q = a.num_c(b);
}
if(c == 4)
{
p = "/";
q = a.num_chu(b);
}
n = q;
frame.getContentPane().add(pane,BorderLayout.CENTER);
button.setMnemonic(KeyEvent.VK_I);
button.addActionListener(new ActionListener(){ //鼠标单击事件
public void actionPerformed(ActionEvent e){
if(n == Integer.parseInt(text.getText().trim()))
label5.setText("正确!");
else
label5.setText("错误!");
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
label1.setText(" "+a.get_num()+" ");
label2.setText(p);
label3.setText(" "+b.get_num()+" ");
frame.setVisible(true);
}
}
git@git.coding.net:amberpass/Calculate.git
https://git.coding.net/amberpass/Calculate.git
程序运行结果:
结对编程:简易四则运算