首页 > 代码库 > 结对编程
结对编程
结对编程:简易四则运算 功能:进行简易的四则运算,并根据给出的结果判断正误。实现:使用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); }}
运算结果:
结对人:王东涵
结对编程
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。