日期:2018.10.07
星期五
博客期:015
Part1:----------------第一个是二柱子出30道小学数学题:
一、程序设计思想
本程序设计由三部分构成,第一部分因为循环30次的需要,做出for语句来循环30次,从流程图中看也清晰明了,所以只需要在循环里输出语句就好了,这一部分分为生成处理和输出处理两部分!生成处理部分,就是利用Random类生成随机数的方法,生成1000以内的两个正整数m和n,在生成随机字符c的时候,先得到0~3的随机数,然后分别代表‘+’、‘-’、‘*’和‘/’四个字符!之后按照格式输出即可!
二、程序流程图
三、源程序
1 package ready; 2 3 import java.util.Random; 4 5 public class Test { 6 //============================<数据区>=================================// 7 //左操作数 8 private int leftNumber; 9 //右操作数 10 private int rightNumber; 11 //运算符 12 private char operator; 13 //============================<方法区>=================================// 14 //--------------------------------主函数 15 public static void main(String[] args) { 16 for(int i=1;i<=30;i++) 17 { 18 Test n = new Test(true); 19 System.out.print(i+"、 "); 20 n.display_s(false); 21 } 22 } 23 //--------------------------------set、get函数 24 //---[operator的set、get函数] 25 public void setOperator(char operator) { 26 this.operator = operator; 27 } 28 public char getOperator() { 29 return operator; 30 } 31 //---[RightNumber的set、get函数] 32 public void setRightNumber(int rightNumber) { 33 this.rightNumber = rightNumber; 34 } 35 public int getRightNumber() { 36 return rightNumber; 37 } 38 //---[LeftNumber的set、get函数] 39 public void setLeftNumber(int leftNumber) { 40 this.leftNumber = leftNumber; 41 } 42 public int getLeftNumber() { 43 return leftNumber; 44 } 45 //--------------------------------展示函数 46 public void display(){ 47 System.out.print(leftNumber+" "+operator+" "+rightNumber); 48 } 49 public void display_s(){ 50 System.out.println(leftNumber+" "+operator+" "+rightNumber); 51 } 52 public void display(boolean x){ 53 if(x) 54 System.out.print(leftNumber+" "+operator+" "+rightNumber+" = "+answer()); 55 else 56 System.out.print(leftNumber+" "+operator+" "+rightNumber+" = "); 57 } 58 public void display_s(boolean x){ 59 if(x) 60 System.out.println(leftNumber+" "+operator+" "+rightNumber+" = "+answer()); 61 else 62 System.out.println(leftNumber+" "+operator+" "+rightNumber+" = "); 63 } 64 //--------------------------------运行函数 65 //返回结果 66 public int answer(){ 67 int result = 0; 68 switch(operator) 69 { 70 case '+':result=leftNumber+rightNumber;break; 71 case '*':result=leftNumber*rightNumber;break; 72 case '/':result=leftNumber/rightNumber;break; 73 case '-':result=leftNumber-rightNumber;break; 74 default:break; 75 } 76 return result; 77 } 78 //--------------------------------构造函数 79 public Test(){ 80 leftNumber = 0; 81 operator = '+'; 82 rightNumber = 0; 83 } 84 public Test(boolean x){ 85 Random ra = new Random(); 86 leftNumber = ra.nextInt(1000)+1; 87 int temp = ra.nextInt(4); 88 switch(temp) 89 { 90 case 0:operator = '+';break; 91 case 1:operator = '-';break; 92 case 2:operator = '*';break; 93 case 3:operator = '/';break; 94 } 95 rightNumber = ra.nextInt(1000)+1; 96 } 97 public Test(int leftnum,char oper,int rightnum){ 98 leftNumber = leftnum; 99 operator = oper; 100 rightNumber = rightnum; 101 } 102 }
四、实现结果截图
五、实验总结
本次程序老师要求在20分钟以内完成,实际上还是存在一些问题的!比如说输出问题,以及小学二年级的数学题目究竟是怎样的!应该如何去遍历这个“软件”,需不需要加入“ = ”,输出格式多一个换行等等的问题都需要解决的!
Part2:----------------第二个是窗口验证码的问题:
一、程序设计思想
本程序只是实现了验证码功能!就难度而言,不难!但是就复杂度而言,算是比较繁杂的了!程序需要制作一个窗口,并对其构成容器,再在容器里添加内部项,添加按钮、添加标签和输入框!对成员逐一进行排版,格式设置!对按钮添加监听器,当按下按钮时,刷新程序的验证码!
二、程序流程图
三、源程序
1 package ready; 2 3 import java.awt.Container; 4 import java.awt.FlowLayout; 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 7 import java.io.IOException; 8 //import java.awt.Image; 9 import java.util.Random; 10 11 //import javax.swing.ImageIcon; 12 import javax.swing.JButton; 13 import javax.swing.JFrame; 14 import javax.swing.JLabel; 15 import javax.swing.JTextField; 16 17 public class Window extends JFrame{ 18 //=============================<数据区>====================================// 19 //---------------[自动生成] 20 private static final long serialVersionUID = -2971058395175770886L; 21 //---------------[截取随机数] 22 protected Random ra = new Random(); 23 //---------------[验证码字符] 24 private final char[] CHAR_CODE = { '1', '2', '3', '4', '5', '6', 25 '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 26 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 27 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 28 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; 29 //---------------[标识] 30 protected JLabel firstSay = new JLabel("登录名:"); 31 protected JLabel secondSay = new JLabel(" 密码:"); 32 protected JLabel thirdSay = new JLabel("验证码:"); 33 //---------------[按钮] 34 protected JButton getIn = new JButton(" 登陆 "); 35 protected JButton setIn = new JButton(" 快速注册 "); 36 protected JButton resettitle = new JButton(" 刷新 "); 37 //---------------[输入框] 38 protected JTextField firstTitle = new JTextField(10); 39 protected JTextField secondTitle = new JTextField(10); 40 protected JTextField thirdTitle = new JTextField(10); 41 protected JTextField rentlyWords = new JTextField(5); 42 //=============================<方法区>====================================// 43 //---------------[主方法] 44 public static void main(String[] args) throws IOException{ 45 new Window(); 46 } 47 //---------------[构造方法] 48 public Window() throws IOException{ 49 this.setVisible(true); 50 Container c = this.getContentPane(); 51 this.setResizable(false); 52 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 53 this.getContentPane().setLayout(new FlowLayout()); 54 this.add(thirdTitle); 55 this.setLocation(1000,500); 56 this.setSize(400,380); 57 this.setTitle("请登录"); 58 JLabel t = new JLabel(" "); 59 firstSay.setFont(new java.awt.Font("Dialog",1,25)); 60 secondSay.setFont(new java.awt.Font("Dialog",1,25)); 61 thirdSay.setFont(new java.awt.Font("Dialog",1,25)); 62 firstTitle.setFont(new java.awt.Font("Dialog",2,25)); 63 secondTitle.setFont(new java.awt.Font("Dialog",2,25)); 64 thirdTitle.setFont(new java.awt.Font("Dialog",2,25)); 65 setIn.setFont(new java.awt.Font("Dialog",1,25)); 66 getIn.setFont(new java.awt.Font("Dialog",1,25)); 67 resettitle.setFont(new java.awt.Font("Dialog",1,25)); 68 rentlyWords.setFont(new java.awt.Font("Dialog",5,25)); 69 firstTitle.setText("用户名/邮箱"); 70 c.add(firstSay); 71 c.add(firstTitle); 72 c.add(t); 73 c.add(secondSay); 74 c.add(secondTitle); 75 t = new JLabel(" "); 76 c.add(t); 77 c.add(thirdSay); 78 c.add(thirdTitle); 79 t = new JLabel(" "); 80 c.add(t); 81 rentlyWords.setEditable(false); 82 Reset(); 83 resettitle.setBorderPainted(false); 84 c.add(rentlyWords); 85 c.add(resettitle); 86 requestFocus(); 87 t = new JLabel(" "); 88 c.add(t); 89 c.add(getIn); 90 t = new JLabel(" "); 91 c.add(t); 92 c.add(setIn); 93 resettitle.addActionListener//为转换按钮添加监听事件 94 ( 95 new ActionListener() 96 { 97 public void actionPerformed(ActionEvent arg0){ 98 Reset(); 99 } 100 } 101 ); 102 } 103 //---------------[返回随机字符] 104 public final char getRandom(){ 105 int temp = ra.nextInt(58); 106 return CHAR_CODE[temp]; 107 } 108 //---------------[设置随机码] 109 public void Reset(){ 110 rentlyWords.setText(getRandom()+""+getRandom()+""+getRandom()+""+getRandom()); 111 } 112 }
四、实现结果截图
五、实验总结
本次实验相当的顺利,虽然没有用老师要求的RandomStr类来自动生成验证码字符串,不过效果是一样的!用Random配合类型转化一样可以生成验证码字符串,对文本框的排版也是很大的问题!因为在这之前的程序我都没有设计过界面的排版!