驾驶员:许佳豪
领航员:吴哲永 博客地址:http://www.cnblogs.com/dbssb/
主类:
1 package 四则运算; 2 3 public class Test { 4 5 public static void main(String[] args) { 6 7 MyFrame a=new MyFrame(); 8 9 } 10 11 }
窗体类:
1 package 四则运算; 2 3 import java.*; 4 import java.util.*; 5 6 import javax.swing.*; 7 8 import java.awt.*; 9 import java.awt.event.*; 10 import java.io.*; 11 12 public class MyFrame extends JFrame 13 { 14 JMenuBar M1=new JMenuBar(); 15 JMenu M2=new JMenu("更换颜色"); 16 JMenuItem M3=new JMenuItem("黄色"); 17 JMenuItem M4=new JMenuItem("橙色"); 18 JMenuItem M5=new JMenuItem("白色"); 19 JMenuItem M6=new JMenuItem("蓝色"); 20 JMenuItem M7=new JMenuItem("绿色"); 21 JLabel L1=new JLabel("请输入题数(最多不超过5题):"); 22 JLabel L2=new JLabel("第一题:"); 23 JLabel L3=new JLabel(" = "); 24 JLabel L4=new JLabel("第二题:"); 25 JLabel L5=new JLabel(" = "); 26 JLabel L6=new JLabel("第三题:"); 27 JLabel L7=new JLabel(" = "); 28 JLabel L8=new JLabel("第四题:"); 29 JLabel L9=new JLabel(" = "); 30 JLabel L10=new JLabel("第五题:"); 31 JLabel L11=new JLabel(" = "); 32 JLabel L12=new JLabel("欢迎您,尊敬的 "); 33 JLabel L13=new JLabel("用户"); 34 JLabel L14=new JLabel(""); 35 JLabel L15=new JLabel(" "); 36 JLabel L16=new JLabel(" "); 37 JLabel L17=new JLabel(" "); 38 JLabel L18=new JLabel(""); 39 JLabel L19=new JLabel(" "); 40 JTextField[]T={ 41 new JTextField(12),new JTextField(12),new JTextField(12),new JTextField(12),new JTextField(12),new JTextField(8),new JTextField(8),new JTextField(8),new JTextField(8),new JTextField(8),new JTextField(5) 42 }; 43 JButton B1=new JButton("开始答题"); 44 JButton B2=new JButton("提交"); 45 46 JFrame f=new JFrame(); 47 48 String username=new String(); 49 int i; 50 int k; 51 int n; 52 long begintime; 53 long endtime; 54 long time; 55 int[]a=new int[5]; 56 int[]b=new int[5]; 57 int[]c=new int[5]; 58 int[]d=new int[5]; 59 int[]answer=new int[5]; 60 61 62 public MyFrame() 63 { 64 getContentPane().setBackground(Color.WHITE); 65 this.setVisible(true); 66 this.setTitle("四则运算答题"); 67 Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); 68 setBounds((d.width - 502) / 2, (d.height - 450) / 2, 502, 450); 69 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 70 this.setResizable(false); 71 getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 12, 10)); 72 setJMenuBar(M1); 73 M1.add(M2); 74 M2.add(M3); 75 M2.add(M4); 76 M2.add(M5); 77 M2.add(M6); 78 M2.add(M7); 79 L12.setForeground(Color.RED); 80 L12.setFont(new Font("宋体", Font.BOLD, 25)); 81 getContentPane().add(L12); 82 L13.setForeground(SystemColor.textHighlight); 83 L13.setFont(new Font("宋体", Font.BOLD, 25)); 84 getContentPane().add(L13); 85 L1.setFont(new Font("宋体", Font.PLAIN, 20)); 86 getContentPane().add(L1); 87 T[10].setFont(new Font("宋体", Font.PLAIN, 20)); 88 getContentPane().add(T[10]); 89 B1.setFont(new Font("宋体", Font.PLAIN, 20)); 90 getContentPane().add(B1); 91 L2.setFont(new Font("宋体", Font.PLAIN, 20)); 92 getContentPane().add(L2); 93 T[0].setFont(new Font("宋体", Font.PLAIN, 20)); 94 getContentPane().add(T[0]); 95 L3.setFont(new Font("宋体", Font.PLAIN, 20)); 96 getContentPane().add(L3); 97 T[5].setFont(new Font("宋体", Font.PLAIN, 20)); 98 getContentPane().add(T[5]); 99 L4.setFont(new Font("宋体", Font.PLAIN, 20)); 100 getContentPane().add(L4); 101 T[1].setFont(new Font("宋体", Font.PLAIN, 20)); 102 getContentPane().add(T[1]); 103 L5.setFont(new Font("宋体", Font.PLAIN, 20)); 104 getContentPane().add(L5); 105 T[6].setFont(new Font("宋体", Font.PLAIN, 20)); 106 getContentPane().add(T[6]); 107 L6.setFont(new Font("宋体", Font.PLAIN, 20)); 108 getContentPane().add(L6); 109 T[2].setFont(new Font("宋体", Font.PLAIN, 20)); 110 getContentPane().add(T[2]); 111 L7.setFont(new Font("宋体", Font.PLAIN, 20)); 112 getContentPane().add(L7); 113 T[7].setFont(new Font("宋体", Font.PLAIN, 20)); 114 getContentPane().add(T[7]); 115 L8.setFont(new Font("宋体", Font.PLAIN, 20)); 116 getContentPane().add(L8); 117 T[3].setFont(new Font("宋体", Font.PLAIN, 20)); 118 getContentPane().add(T[3]); 119 L9.setFont(new Font("宋体", Font.PLAIN, 20)); 120 getContentPane().add(L9); 121 T[8].setFont(new Font("宋体", Font.PLAIN, 20)); 122 getContentPane().add(T[8]); 123 L10.setFont(new Font("宋体", Font.PLAIN, 20)); 124 getContentPane().add(L10); 125 T[4].setFont(new Font("宋体", Font.PLAIN, 20)); 126 getContentPane().add(T[4]); 127 L11.setFont(new Font("宋体", Font.PLAIN, 20)); 128 getContentPane().add(L11); 129 T[9].setFont(new Font("宋体", Font.PLAIN, 20)); 130 getContentPane().add(T[9]); 131 L14.setFont(new Font("宋体", Font.PLAIN, 20)); 132 getContentPane().add(L14); 133 L19.setFont(new Font("宋体", Font.PLAIN, 20)); 134 getContentPane().add(L19); 135 B2.setFont(new Font("宋体", Font.PLAIN, 20)); 136 getContentPane().add(B2); 137 L15.setFont(new Font("宋体", Font.PLAIN, 20)); 138 getContentPane().add(L15); 139 L16.setFont(new Font("宋体", Font.PLAIN, 20)); 140 getContentPane().add(L16); 141 L14.setFont(new Font("宋体", Font.PLAIN, 20)); 142 getContentPane().add(L14); 143 L17.setFont(new Font("宋体", Font.PLAIN, 20)); 144 getContentPane().add(L17); 145 L18.setFont(new Font("宋体", Font.PLAIN, 20)); 146 getContentPane().add(L18); 147 148 File file=new File("用户名.txt"); 149 if(!file.exists()) 150 { 151 try 152 { 153 file.createNewFile(); 154 } 155 catch (IOException e) 156 { 157 e.printStackTrace(); 158 } 159 Inputname b=new Inputname(); 160 } 161 else 162 { 163 String str1=new String("");; 164 int i=0; 165 BufferedReader br = null; 166 try 167 { 168 br = new BufferedReader(new FileReader(file.getName())); 169 } 170 catch (FileNotFoundException e1) 171 { 172 e1.printStackTrace(); 173 } 174 try 175 { 176 while((str1=br.readLine())!=null) 177 { 178 username=str1; 179 } 180 } 181 catch (IOException e) 182 { 183 e.printStackTrace(); 184 } 185 try 186 { 187 br.close(); 188 } 189 catch (IOException e) 190 { 191 e.printStackTrace(); 192 } 193 if(username.length()==0) 194 { 195 Inputname b=new Inputname(); 196 } 197 else 198 { 199 L13.setText(username); 200 } 201 } 202 203 B1.addActionListener(new Action()); 204 B2.addActionListener(new Action()); 205 M3.addActionListener(new Action()); 206 M4.addActionListener(new Action()); 207 M5.addActionListener(new Action()); 208 M6.addActionListener(new Action()); 209 M7.addActionListener(new Action()); 210 } 211 212 public class Action implements ActionListener 213 { 214 215 public void actionPerformed(ActionEvent e) 216 { 217 try 218 { 219 if(e.getSource()==B1) 220 { 221 for(k=0;k<10;k++) 222 { 223 T[k].setText(null); 224 } 225 L15.setText(" "); 226 L16.setText(null); 227 L17.setText(null); 228 L18.setText(""); 229 i=0; 230 231 if(T[10].getText().length()==0) 232 { 233 JOptionPane.showMessageDialog(f,"请输入题数!"); 234 } 235 else if(Integer.parseInt(T[10].getText())>5) 236 { 237 JOptionPane.showMessageDialog(f,"题数最多不超过5!"); 238 } 239 else 240 { 241 i=Integer.parseInt(T[10].getText()); 242 243 for(k=0;k<i;k++) 244 { 245 a[k]=(int)(Math.random()*201-100); 246 b[k]=(int)(Math.random()*201-100); 247 c[k]=(int)(Math.random()*5); 248 d[k]=(int)(Math.random()*12+1); 249 Core core=new Core(a[k],b[k],c[k],d[k]); 250 answer[k]=core.calc(); 251 T[k].setText(core.toString()); 252 253 } 254 begintime = System.currentTimeMillis(); 255 } 256 } 257 }catch(NumberFormatException u) 258 { 259 JOptionPane.showMessageDialog(f,"请输入正确的数字!"); 260 } 261 262 if(e.getSource()==B2) 263 { 264 if(i==0) 265 { 266 JOptionPane.showMessageDialog(f,"请先进行答题!"); 267 } 268 else 269 { 270 endtime = System.currentTimeMillis(); 271 time=(endtime-begintime)/1000; 272 n=0; 273 for(k=0;k<i;k++) 274 { 275 if(T[k+5].getText().length()==0) 276 { 277 continue; 278 } 279 try 280 { 281 if(answer[k]==Integer.parseInt(T[k+5].getText())) 282 { 283 n++; 284 } 285 }catch(NumberFormatException u) 286 { 287 JOptionPane.showMessageDialog(f,"请输入正确的数字!"); 288 } 289 290 } 291 L15.setText("你答对了:"+n+"道题"); 292 L16.setText("你答错了:"+(i-n)+"道题"); 293 L14.setText("用时:"+time+"秒"); 294 L17.setText("参考答案:"); 295 for(k=0;k<i;k++) 296 { 297 L18.setText(L18.getText()+answer[k]+";"); 298 } 299 i=0; 300 } 301 } 302 303 if(e.getSource()==M3) 304 { 305 getContentPane().setBackground(Color.YELLOW); 306 } 307 if(e.getSource()==M4) 308 { 309 getContentPane().setBackground(Color.ORANGE); 310 } 311 if(e.getSource()==M5) 312 { 313 getContentPane().setBackground(Color.WHITE); 314 } 315 if(e.getSource()==M6) 316 { 317 getContentPane().setBackground(Color.BLUE); 318 } 319 if(e.getSource()==M7) 320 { 321 getContentPane().setBackground(Color.GREEN); 322 } 323 } 324 325 326 } 327 328 public class Inputname extends JFrame 329 { 330 JLabel L1=new JLabel("请输入您的用户名:"); 331 JTextField T1=new JTextField(17); 332 JButton B1=new JButton("确定"); 333 JButton B2=new JButton("关闭"); 334 335 JFrame f=new JFrame(); 336 337 public Inputname() 338 { 339 this.setVisible(true); 340 this.setTitle("输入用户名"); 341 Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); 342 setBounds((d.width - 241) / 2, (d.height - 138) / 2, 242, 146); 343 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 344 this.setResizable(false); 345 getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 20, 6)); 346 L1.setFont(new Font("宋体", Font.PLAIN, 20)); 347 getContentPane().add(L1); 348 T1.setFont(new Font("宋体", Font.PLAIN, 20)); 349 getContentPane().add(T1); 350 B1.setFont(new Font("宋体", Font.PLAIN, 20)); 351 getContentPane().add(B1); 352 B2.setFont(new Font("宋体", Font.PLAIN, 20)); 353 getContentPane().add(B2); 354 B1.addActionListener(new action()); 355 B2.addActionListener(new action()); 356 } 357 358 public class action implements ActionListener 359 { 360 361 public void actionPerformed(ActionEvent e) 362 { 363 if(e.getSource()==B2) 364 { 365 setVisible(false); 366 } 367 368 if(e.getSource()==B1) 369 { 370 if(T1.getText().length()==0) 371 { 372 JOptionPane.showMessageDialog(f,"用户名不能为空"); 373 } 374 else 375 { 376 username=T1.getText(); 377 L13.setText(username); 378 379 File file=new File("用户名.txt"); 380 try{ 381 BufferedWriter writer=new BufferedWriter(new FileWriter(file.getName())); 382 writer.write(username);; 383 writer.close(); 384 } catch(IOException g) 385 { 386 System.out.print(g); 387 } 388 389 setVisible(false); 390 } 391 392 } 393 394 } 395 396 } 397 398 } 399 400 }
核心类:
1 package 四则运算; 2 3 public class Core { 4 int a; 5 int b; 6 int c; 7 int d; 8 public Core(int a,int b,int c,int d) 9 { 10 this.a=a; 11 this.b=b; 12 this.c=c; 13 this.d=d; 14 } 15 public int calc() 16 { 17 if(c==0) 18 { 19 return a+b; 20 } 21 else if(c==1) 22 { 23 return a-b; 24 } 25 else if(c==2) 26 { 27 return a*b; 28 } 29 else if(c==3) 30 { 31 if(b==0) 32 { 33 b++; 34 } 35 return a/b; 36 } 37 else 38 { 39 return jiecheng(d); 40 } 41 } 42 public String toString() 43 { 44 String A=new String(); 45 String B=new String(); 46 if(a<0) 47 { 48 A="("+a+")"; 49 } 50 else 51 { 52 A=a+""; 53 } 54 if(b<0) 55 { 56 B="("+b+")"; 57 } 58 else 59 { 60 B=b+""; 61 } 62 if(c==0) 63 { 64 return A+"+"+B; 65 } 66 else if(c==1) 67 { 68 return A+"-"+B; 69 } 70 else if(c==2) 71 { 72 return A+"*"+B; 73 } 74 else if(c==3) 75 { 76 return A+"/"+B; 77 } 78 else 79 { 80 return d+"!"; 81 } 82 } 83 84 public int jiecheng(int x) 85 { 86 if(x==0 || x==1) 87 { 88 return 1; 89 } 90 else 91 { 92 return x*jiecheng(x-1); 93 } 94 } 95 }
Junit测试用例:
1 package 四则运算; 2 3 import static org.junit.Assert.*; 4 5 import org.junit.Test; 6 7 public class CoreTest { 8 Core test1=new Core(-75,15,0,10); 9 Core test2=new Core(-75,15,1,10); 10 Core test3=new Core(-75,15,2,10); 11 Core test4=new Core(-75,15,3,10); 12 Core test5=new Core(-75,15,4,10); 13 14 @Test 15 public void testCalc() { 16 System.out.println(test1.calc()); 17 System.out.println(test2.calc()); 18 System.out.println(test3.calc()); 19 System.out.println(test4.calc()); 20 System.out.println(test5.calc()); 21 } 22 23 }
测试运行截图:
0个错误,成功!