结对编程队友的个人项目代码分析

他的代码是用java在eclipse中编写的,基本实现了老师所要求的功能先说一下优点,优点就是代码精简,一百三十行完成,与我自己的代码相比,尤其是在生成题目的函数上,无疑更加精简并且算法巧妙,其函数只用一个n作为参数用来调控三种不同难度的题目转换,但精简的部分缺点就是生成题目方式比较单一,比如在括号的应用上,无法生成对整个括号进行乘方开方的题目。综合来说其整篇代码都是这种高效的风格,其中思路让我这种脑子不好且水平不高的人有所受益,但他的代码缺点也很明显,写代码的习惯有些差,比较杂乱且没有注释,通篇只有主类,用户信息等也是直接用string保存,感觉如果要更改需求或者升级等,改动代码的成本会很高,测试之后有一个问题就是在TxT中无法进行换行,(经过查询发现是TxT无法识别“\n\n”标识符)

总的来说还不错。(相比我后来发现自己的代码bug百出,是不错了)

//目标文件夹为桌面上的test文件夹,运行之前请在桌面上新建一个名为test的文件夹
package first;
import java.util.*;
import java.io.*;
import java.text.SimpleDateFormat;
public class CreatProblem {
public static String[] operator= {"+","-","*","/","^2","^0.5","sin","cos","tan"};
public static String[][] Users= {{"张三1","123","小学"},{"张三2","123","小学"},{"张三3","123","小学"},
{"李四1","123","初中"},{"李四2","123","初中"},{"李四3","123","初中"},
{"王五1","123","高中"},{"王五2","123","高中"},{"王五3","123","高中"}};
public static String GetProblem(int n) {
String str="";
boolean isBrackets;
int Brackets=0;
int x;
int Operand=(int)(Math.random()*4)+2;
for(int a=0;a<Operand;a++) {
x=(int)(Math.random()*2);
if((x==1)&(a<Operand-1)&(a>0)) {
Brackets++;
str=str+'(';
isBrackets=true;
}
else isBrackets=false;
x=(int)(Math.random()*4+n);
if((x>5)&(x<9)) str=str+operator[x];
str=str+String.valueOf((int)(Math.random()*101));
x=(int)(Math.random()*4+n);
if((x>3)&(x<6)) str=str+operator[x];
x=(int)(Math.random()*2);
if((x==1)&(Brackets>0)&(isBrackets=false)) {
str=str+')';
Brackets--;
}
if(a<Operand-1) str=str+operator[(int)(Math.random()*4)];
else for(int t=0;t<Brackets;t++) {
str=str+')';
}
}
return str;
}
public static void GetTestPaper(String Name,int num,int level) {
Date getDate=Calendar.getInstance().getTime();
String Path=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(getDate);
Path="C:\\Users\\asus\\Desktop\\test\\"+Name+"\\"+Path+".txt";
File file=new File(Path);
File fileParent=file.getParentFile();
if(!fileParent.exists()) fileParent.mkdirs();
try {
if(!file.exists()) file.createNewFile();
FileWriter fw=new FileWriter(file);
for(int a=0;a<num;a++) {
fw.write(GetProblem(level)+"\n\n");
fw.flush();
}
fw.close();
}catch(IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("请输入用户名和密码,中间用空格隔开:");
int level=0;
String name;
String password;
Circle:
while(true) {
name=sc.next();
password=sc.next();
for(int a=0;a<9;a++) {
if((Users[a][0].equals(name)==true)&(Users[a][1].equals(password)==true)) break Circle;
if(a==8) System.out.println("请输入正确的用户名和密码,中间用空格隔开:");
}
}
switch(name) {
case "张三1":name="zhangsan1";
level=0;
break;
case "张三2":name="zhangsan2";
level=0;
break;
case "张三3":name="zhangsan3";
level=0;
break;
case "李四1":name="lisi1";
level=2;
break;
case "李四2":name="lisi2";
level=2;
break;
case "李四3":name="lisi3";
level=2;
break;
case "王五1":name="wangwu1";
level=5;
break;
case "王五2":name="wangwu2";
level=5;
break;
case "王五3":name="wangwu3";
level=5;
break;
}
while(true) {
System.out.println("准备生成"+Users[level+1][2]+"题目,继续请输入‘y’,否则请输入需要的出题类型(小学、初中、高中):");
String choise=sc.next();
if(choise.equals("y")==true) {
System.out.println("请输入出题数量:");
int num=sc.nextInt();
GetTestPaper(name,num,level);
}
else {
switch(choise) {
case "小学":level=0;
break;
case "初中":level=2;
break;
case "高中":level=5;
break;
default:System.out.println("错误输入,请注意格式;");
}
}
}
}

}

猜你喜欢

转载自www.cnblogs.com/fyq-kylin/p/9716183.html