不同于我选择C++来实现个人项目,我的队友使用了Java来解决题目,其中包括注册,产生题目和文件输出三个部分,下面我们来看一下代码的优缺点。
优点:
1.代码习惯
可以看到,我的队友的代码习惯非常良好,代码中变量的名字均有实际含义而非“x,y,z”这类无意义的字母
代码结构也很标准,视觉上感觉很舒服。
package zuoye; public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
2.代码优化
可以看到我的队友对他的代码进行了优化,从而保证了代码运行的效率。这是一个很好的编程习惯,如果代码不进行优化的话,随着代码量的不断增加,整个程序的运行效率也会越来越低,最终被淘汰掉。
3.代码功能
我队友的代码整体上大概实现了需求中要求的功能,同时自己增加了一个简单的UI界面,也为我们结对编程完成项目打下了基础。
缺点:功能实现还有些小bug
1.初中题目中对于平方和根号的处理,不能出现在同一个位置,只能分开放,这样的话题目出的就会少了一些情况,不够全面。
2.对于括号的处理,无法实现括号只在需要出现的地方出现,即有时候会出现“无效”括号,括号有没有对于计算结果不产生影响。
总结:
可以看出,我的队友编程基础还是比较好的,希望他可以带飞我(最好可以采用极限编程,我极限他编程那种)。
附代码:
package zuoye; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.Label; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class CalFrame extends JFrame{ public void showUI(String username,String password) { FirstProject f=new FirstProject(); this.setTitle("界面"); this.setSize(800, 600); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLayout(new GridLayout()); String [] shapeArr= {"小学","初中","高中"}; JTextField [] jtf=new JTextField [3]; CalListener l=new CalListener(); l.setG(username, password); JLabel label=new JLabel("欢迎来到试卷生成系统,请在对应按钮下输入试卷题目数量,将自动在"+f.iniD+"目录上创建"+ username+"文件夹和相应试卷"); this.add(label); setLayout(new GridLayout(8,1)); getContentPane().add(label); for(int i=0;i<shapeArr.length;i++) { JButton btn=new JButton(shapeArr[i]); jtf[i]=new JTextField(20); getContentPane().add(btn); getContentPane().add(jtf[i]); btn.addActionListener(l); } l.setGrade(jtf,shapeArr); this.setVisible(true); } }
package zuoye; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import javax.swing.JTextField; public class CalListener implements ActionListener{ String Arr=""; FirstProject f=new FirstProject(); String un,pwd; JTextField [] jtf; String [] shapeArr; public void setG(String un,String pwd) { this.un=un; this.pwd=pwd; } public void setGrade(JTextField [] jtf,String [] shapeArr) { this.jtf=jtf; this.shapeArr=shapeArr; } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub Arr=e.getActionCommand(); int g=0; for(int i=0;i<3;i++) { if(shapeArr[i].equals(Arr)) { if("".equals(jtf[i].getText()))System.out.println("输入错误"); else g=Integer.parseInt(jtf[i].getText()); } } if(g!=0) { try { f.work(un, pwd, Arr,g); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } }
package zuoye; import java.sql.DriverManager; import java.sql.ResultSet; import com.mysql.jdbc.Connection; import com.mysql.jdbc.PreparedStatement; import com.mysql.jdbc.Statement; public class ConnDB { public Connection conn=null; public PreparedStatement stmt=null; public ResultSet rs=null; //驱动类的类名 private static String dbClassName="com.mysql.jdbc.Driver"; private static String dbUrl="jdbc:mysql://localhost:3306/usercontroldb"; private static String username="root"; private static String password="123456"; public boolean findUser(String un,String pwd) { User u=null; try { conn=(Connection) DriverManager.getConnection(dbUrl, username, password); String sql="SELECT * FROM teacher WHERE username=? and userpassword=?"; stmt=(PreparedStatement) conn.prepareStatement(sql); stmt.setString(1,un); stmt.setString(2,pwd); rs=stmt.executeQuery(); if(rs.next()) { u=new User(); u.setUsername(rs.getString(2)); u.setPassword(rs.getString(3)); } }catch (Exception e) { // TODO: handle exception e.printStackTrace(); } if(u.getUsername()!=null&&u.getPassword()!=null) { return true; } return false; } // public static void main(String [] args) { // String name="张三1"; // String pwd="123"; // ConnDB connDB=new ConnDB(); // if(connDB.findUser(name, pwd))System.out.println("1"); // System.out.println(u.getUsername()); // System.out.println(u.getPassword()); // } }
package zuoye; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintStream; import java.io.UnsupportedEncodingException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; import java.util.Scanner; public class FirstProject { public static Random random=new Random(); private char[] sign1= {‘+‘,‘-‘,‘*‘,‘/‘};//符号 private char[] sign2= {‘√‘,‘^‘,‘%‘};//根号,平方和,取余(可加可不加) private String[] sign3= {"sin","cos","tan"};//三角函数 private static String [] grade={"小学","初中","高中"}; int p=3;//算式符号出现的概率1/3 String iniD="D:/java/calculate/";//初始目录 //work方法用以执行整个过程 public void work(String un,String pwd,String g,int n) throws IOException { Scanner scanner=new Scanner(System.in); FirstProject f=new FirstProject(); //做计算//创建文件 SimpleDateFormat dFormat=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); String title=dFormat.format(new Date()).toString(); File file=new File(iniD+un+"/"); if(!file.exists()) { file.mkdirs(); } file=new File(iniD+un+"/"+title+".txt"); FileOutputStream out=new FileOutputStream(file) ; PrintStream printToFile=new PrintStream(out); PrintStream printToConsole=System.out; f.prepare(g); if(!f.judgeover(n)) System.out.println("数量应在10~30,请重新输入"); //第一次题目的输出 else f.result(f, n, g, printToFile, printToConsole); //System.out.println("请输入“切换为xx”或者“继续输入”"); } public void result(FirstProject f,int n,String g,PrintStream printToFile,PrintStream printToConsole) throws IOException { System.out.println("开始输出到文件"); System.setOut(printToFile); f.calculate(4,n,g); System.setOut(printToConsole); System.out.println("输出成功"); } public void prepare(String g) { Scanner scanner=new Scanner(System.in); System.out.println("当前选择为"+g+"出题"); System.out.println("准备生成"+g+"数学题目,请输入生成题目数量:"); } public boolean judgeover(int n) {//判定题数是否合适 if(n<10||n>30) { return false; } return true; } public void calculate(int si,int n,String g) {//计算过程 si表示sign1用到的符号个数,n表示数字个数 System.out.println(g+"试卷"); for(int i=0;i<n;i++) { boolean [] cou= {false,false,false};//用以判定直到最后是否出现了根号,三角函数,如果没有,就让最后一个数带上 int count=random.nextInt(2)+3;//操作数的个数 System.out.print(i+1+"、"); for(int j=0;j<count;j++) { int number=random.nextInt(100)+1; int number2=random.nextInt(100)+1; int temp=0; int bracket=random.nextInt(p);//随机出现括号,概率为1/3 int sqrt=random.nextInt(p); int square=random.nextInt(p); int tri=random.nextInt(p); if(bracket==2) {//加括号的情况输出类似于(x+y)- bracket(number, number2, temp, si); System.out.print(sign1[temp]); j++; } if(g.equals(grade[1])&&(sqrt==2)){//只输出根号 sqrt(); cou[1]=true; } if(g.equals(grade[2])&&tri==2) {//只输出三角函数符号 trigonometric(); cou[2]=true; } if(j==count-1&&g.equals(grade[2])&&!cou[2])trigonometric(); System.out.print(number); if(g.equals(grade[1])&&(square==2)){//只输出平方 square(); // cou[3]=true; } temp=random.nextInt(si); if(j<count-1) System.out.print(sign1[temp]);//保证最后一个符号是数字 // if(j==count-1&&g.equals(grade[1])&&!cou[1]&&!cou[3])square(); } System.out.println("="); System.out.println(); } } public void bracket(int number,int number2,int temp,int sig) { System.out.print("("); System.out.print(number); temp=random.nextInt(sig); System.out.print(sign1[temp]); System.out.print(number2); System.out.print(")"); } //根号和平方位置不同,只能分开,目前不知道怎么办,很烦 public void sqrt() { System.out.print(sign2[0]); } public void square() { System.out.print(sign2[1]); } public void trigonometric() { int temp=random.nextInt(3); System.out.print(sign3[temp]); } }
package zuoye; import java.awt.Container; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.TextField; import java.net.PasswordAuthentication; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; public class LoginFrame extends JFrame{ public void showUI() { this.setTitle("界面"); this.setSize(400, 300); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // this.setLayout(new FlowLayout()); this.setLayout(new GridLayout()); Container container=getContentPane(); container.setLayout(new GridLayout(4, 2,10,10)); JLabel label=new JLabel("用户名"); JTextField username=new JTextField(50); container.add(label); container.add(username); JLabel label2=new JLabel("密码"); JPasswordField password=new JPasswordField(100); this.add(label2); this.add(password); LoginListener login=new LoginListener(); login.setC(username, password);//传用户名和密码 JButton btn1=new JButton("登录"); JButton btn2=new JButton("注册"); btn1.addActionListener(login); btn2.addActionListener(login); this.add(btn1); this.add(btn2); this.setVisible(true); } public static void main(String[] args) { LoginFrame l=new LoginFrame(); l.showUI(); } }
package zuoye; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.net.PasswordAuthentication; import javax.swing.JPasswordField; import javax.swing.JTextField; public class LoginListener implements ActionListener{ String [] UserName= {"张三1","张三2","张三3","李四1","李四2","李四3","王五1","王五2","王五3"}; FirstProject f=new FirstProject(); CalFrame cFrame=new CalFrame(); String Arr=""; JTextField username; JPasswordField password; public void setC(JTextField username,JPasswordField password) { this.username=username; this.password=password; } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub Arr=e.getActionCommand(); String un=username.getText(); String pwd=String.valueOf(password.getPassword());//获取密码的方式比较特殊 System.out.println(un+pwd); ConnDB connDB=new ConnDB(); if("登录".equals(Arr)) { //这一行if用以在数据库中查找,如果不连接mysql,就注释,在数组中查找 //if(connDB.findUser(un, pwd)) for(int i=0;i<9;i++) { if(un.equals(UserName[i])&&pwd.equals("123")) { cFrame.showUI(un,pwd); break; } else if(i==8)System.out.println("用户名或密码错误"); } } } }
package zuoye; public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
原文:https://www.cnblogs.com/Need-GF/p/9716645.html