软件工程作业03
题目:自动生成小学算数题,且有要求。(是否有乘除法,除法是否有余数,生成带括号的式子,连接数据库等)
设计思想:函数包括最大公约数做小公倍数的函数getGcd(double m,double n),getLcm(double m,double n),生成式子数量的函数int num(),生成三个数的式子的函数String sgongshi(int r,int q),生成整数和分数式子的函数String function(int r ,int q, int fun),String fun(int r ,int fun)。在主函数中进行调用这些函数。生成整数和分数的式子函数中用随机数进行符号的选取和运算数的生成,用if和while语句进行条件的判断,生成三个数的式子的函数中调用了生成整数式子的函数,给结果加括号后再随机生成一个运算符和随机数进行组合运算。(查重没有实现1+2=2+1!)
源代码:
package suanshuti; import java.sql.Connection; import java.sql.DriverManager; import java.util.Scanner; public class Timu3 { //保存整数结果 static int re; //保存整数带余数的结果 static String re1; //保存分式结果 static String refenshu; //保存三个整数公式的结果 static int resan; //****最大公约**** static double getGcd(double m,double n){ while(n > 0){ double temp = m % n; m = n; n = temp; } return m; } //****最大公约**** //***最小公倍数*** static double getLcm(double m,double n){ double gcd = getGcd(m,n); double result = m*n / gcd; return result; } //***最小公倍数*** //*****题目数量的函数***** static int num() { int num; Scanner in = new Scanner(System.in); System.out.print("请输入题目的数量:"); num = in.nextInt(); return num; } //*****题目数量的函数***** //*****生成三个数的式子的函数****** static String sgongshi(int r,int q) { String shi,shizi=""; int a=(int)(Math.random()*(r)%(r-q+1)+q); int e=(int)(Math.random()*4); shi=function(r,q,12); if(e==0) { int f=(int)(Math.random()*4); if(f==1||f==0) { shizi=a+" + "+"( "+shi+" )"; resan=a+re; } if(f==2||f==3) { shizi="( "+shi+" )"+" + "+a; resan=a+re; } } if(e==1) { int f=(int)(Math.random()*4); if(f==1||f==0) { while(a-re<0) { shi=function(r,q,12); } shizi=a+" - "+"( "+shi+" )"; resan=a-re; } if(f==2||f==3) { while(a-re>0) { shi=function(r,q,12); } shizi="( "+shi+" )"+" - "+a; resan=re-a; } } if(e==2) { int f=(int)(Math.random()*4); if(f==1||f==0) { shizi=a+" × "+"( "+shi+" )"; resan=a*re; } if(f==2||f==3) { shizi="( "+shi+" )"+" × "+a; resan=a*re; } } if(e==3) { int f=(int)(Math.random()*4); if(f==1||f==0) { while(re==0) { shi=function(r,q,12); } while(a%re!=0) { shi=function(r,q,12); while(re==0) { shi=function(r,q,12); } } shizi=a+" ÷ "+"( "+shi+" )"; resan=a/re; } if(f==2||f==3) { while(a==0) { a=(int)(Math.random()*(r)%(r-q+1)+q); } while(re%a!=0) { shi=function(r,q,12); } shizi="( "+shi+" )"+" ÷ "+a; resan=re/a; } } return shizi; } //*****生成三个数的式子的函数****** //*****************************生成分数式子的函数************************************************************ static String fun(int r ,int fun) { //1有乘除法2没有乘除法 String fu =""; String shizi=""; int a=(int)(Math.random()*(r+1)); int b=(int)(Math.random()*(r+1)); int c=(int)(Math.random()*(r+1)); int d=(int)(Math.random()*(r+1)); int e=(int)(Math.random()*4); //1有乘除法****1 if(fun==1) { if(e==0) { fu="+"; while(a>=b||c>=d||b==0||d==0) { a=(int)(Math.random()*(r+1)); b=(int)(Math.random()*(r+1)); c=(int)(Math.random()*(r+1)); d=(int)(Math.random()*(r+1)); } //约分 int aa=(int) getGcd(a,b); int cc=(int) getGcd(c,d); a=a/aa; b=b/aa; c=c/cc; d=d/cc; shizi=a+"/"+b+" "+fu+" "+c+"/"+d; int gongbei=(int) getLcm(b,d); a=a*(gongbei/b); c=c*(gongbei/d); a=a+c; int t=(int) getGcd(a,gongbei); a=a/t; gongbei=gongbei/t; if(a==gongbei) refenshu="1"; else if(a==0) refenshu="0"; else if(a>gongbei) { int s=a%gongbei; refenshu=1+"’"+s+"/"+gongbei; } else refenshu=a+"/"+gongbei; } if(e==1) { fu="-"; double y=getLcm(b,d); double a1=y/b; double c1=y/d; double a2=a*a1; double c2=c*c1; while(a>=b||c>=d||b==0||d==0||a2<c2) { a=(int)(Math.random()*(r+1)); b=(int)(Math.random()*(r+1)); c=(int)(Math.random()*(r+1)); d=(int)(Math.random()*(r+1)); y=getLcm(b,d); a1=y/b; c1=y/d; a2=a*a1; c2=c*c1; } int aa=(int) getGcd(a,b); int cc=(int) getGcd(c,d); a=a/aa; b=b/aa; c=c/cc; d=d/cc; shizi=a+"/"+b+" "+fu+" "+c+"/"+d; int gongbei=(int) getLcm(b,d); a=a*(gongbei/b); c=c*(gongbei/d); a=a-c; int t=(int) getGcd(a,gongbei); a=a/t; gongbei=gongbei/t; if(a==gongbei) refenshu="1"; else if(a==0) refenshu="0"; else refenshu=a+"/"+gongbei; } if(e==2) { fu="×"; while(a>=b||c>=d||b==0||d==0) { a=(int)(Math.random()*(r+1)); b=(int)(Math.random()*(r+1)); c=(int)(Math.random()*(r+1)); d=(int)(Math.random()*(r+1)); } int aa=(int) getGcd(a,b); int cc=(int) getGcd(c,d); a=a/aa; b=b/aa; c=c/cc; d=d/cc; shizi=a+"/"+b+" "+fu+" "+c+"/"+d; a=a*c; b=b*d; int flag1=(int) getGcd(a,b); a=a/flag1;b=b/flag1; if(a==0) refenshu="0"; else refenshu=a+"/"+b; } if(e==3) { fu="÷"; while(a>=b||c>=d||b==0||d==0||c==0) { a=(int)(Math.random()*(r+1)); b=(int)(Math.random()*(r+1)); c=(int)(Math.random()*(r+1)); d=(int)(Math.random()*(r+1)); } int aa=(int) getGcd(a,b); int cc=(int) getGcd(c,d); a=a/aa; b=b/aa; c=c/cc; d=d/cc; shizi=a+"/"+b+" "+fu+" "+c+"/"+d; a=a*d; b=b*c; int flag1=(int) getGcd(a,b); a=a/flag1;b=b/flag1; if(a==0) refenshu="0"; else if(a==b) refenshu="1"; else if(b==1) refenshu=a+""; else if(a>b) { int s=a%b; int s1=a/b; refenshu=s1+"’"+s+"/"+b; } else refenshu=a+"/"+b; } } //2没有有乘除法***2 if(fun==2) { if(e==0) { fu="+"; while(a>=b||c>=d||b==0||d==0) { a=(int)(Math.random()*(r+1)); b=(int)(Math.random()*(r+1)); c=(int)(Math.random()*(r+1)); d=(int)(Math.random()*(r+1)); } int aa=(int) getGcd(a,b); int cc=(int) getGcd(c,d); a=a/aa; b=b/aa; c=c/cc; d=d/cc; shizi=a+"/"+b+" "+fu+" "+c+"/"+d; int gongbei=(int) getLcm(b,d); a=a*(gongbei/b); c=c*(gongbei/d); a=a+c; int t=(int) getGcd(a,gongbei); a=a/t; gongbei=gongbei/t; if(a==gongbei) refenshu="1"; else if(a==0) refenshu="0"; else if(a>gongbei) { int s=a%gongbei; refenshu=1+"’"+s+"/"+gongbei; } else refenshu=a+"/"+gongbei; } if(e==1) { fu="-"; double y=getLcm(b,d); double a1=y/b; double c1=y/d; double a2=a*a1; double c2=c*c1; while(a>=b||c>=d||b==0||d==0||a2<c2) { a=(int)(Math.random()*(r+1)); b=(int)(Math.random()*(r+1)); c=(int)(Math.random()*(r+1)); d=(int)(Math.random()*(r+1)); y=getLcm(b,d); a1=y/b; c1=y/d; a2=a*a1; c2=c*c1; } int aa=(int) getGcd(a,b); int cc=(int) getGcd(c,d); a=a/aa; b=b/aa; c=c/cc; d=d/cc; shizi=a+"/"+b+" "+fu+" "+c+"/"+d; int gongbei=(int) getLcm(b,d); a=a*(gongbei/b); c=c*(gongbei/d); a=a-c; int t=(int) getGcd(a,gongbei); a=a/t; gongbei=gongbei/t; if(a==gongbei) refenshu="1"; else if(a==0) refenshu="0"; else refenshu=a+"/"+gongbei; } if(e==2) { fu="+"; while(a>=b||c>=d||b==0||d==0) { a=(int)(Math.random()*(r+1)); b=(int)(Math.random()*(r+1)); c=(int)(Math.random()*(r+1)); d=(int)(Math.random()*(r+1)); } int aa=(int) getGcd(a,b); int cc=(int) getGcd(c,d); a=a/aa; b=b/aa; c=c/cc; d=d/cc; shizi=a+"/"+b+" "+fu+" "+c+"/"+d; int gongbei=(int) getLcm(b,d); a=a*(gongbei/b); c=c*(gongbei/d); a=a+c; int t=(int) getGcd(a,gongbei); a=a/t; gongbei=gongbei/t; if(a==gongbei) refenshu="1"; else if(a==0) refenshu="0"; else if(a>gongbei) { int s=a%gongbei; refenshu=1+"’"+s+"/"+gongbei; } else refenshu=a+"/"+gongbei; } if(e==3) { fu="-"; double y=getLcm(b,d); double a1=y/b; double c1=y/d; double a2=a*a1; double c2=c*c1; while(a>=b||c>=d||b==0||d==0||a2<c2) { a=(int)(Math.random()*(r+1)); b=(int)(Math.random()*(r+1)); c=(int)(Math.random()*(r+1)); d=(int)(Math.random()*(r+1)); y=getLcm(b,d); a1=y/b; c1=y/d; a2=a*a1; c2=c*c1; } int aa=(int) getGcd(a,b); int cc=(int) getGcd(c,d); a=a/aa; b=b/aa; c=c/cc; d=d/cc; shizi=a+"/"+b+" "+fu+" "+c+"/"+d; int gongbei=(int) getLcm(b,d); a=a*(gongbei/b); c=c*(gongbei/d); a=a-c; int t=(int) getGcd(a,gongbei); a=a/t; gongbei=gongbei/t; if(a==gongbei) refenshu="1"; else if(a==0) refenshu="0"; else refenshu=a+"/"+gongbei; } } return shizi; } //************生成分数式子的函数************************* //***************生成整数公式的函数********************** static String function(int r ,int q, int fun) { //1有乘除法 2加减无负数 3除法无余数(12,1,0) String fu =""; String shizi=""; int a=(int)(Math.random()*(r)%(r-q+1)+q); int b=(int)(Math.random()*(r)%(r-q+1)+q); int c=(int)(Math.random()*4); //1乘除法 2除法无余数****12 if(fun==12) { if(c==0) { fu="+"; shizi=a+" "+fu+" "+b; re=a+b; re1=String.valueOf(re); } if(c==1) { fu="-"; //排除负数可能性 while(a-b<0) { a=(int)(Math.random()*(r)%(r-q+1)+q); b=(int)(Math.random()*(r)%(r-q+1)+q); } shizi=a+" "+fu+" "+b; re=a-b; re1=String.valueOf(re); } if(c==2) { fu="×"; shizi=a+" "+fu+" "+b; re=a*b; re1=String.valueOf(re); } if(c==3) { fu="÷"; //排除分母为0的可能性和余数的可能性 while(b==0||a%b!=0) { a=(int)(Math.random()*(r)%(r-q+1)+q); b=(int)(Math.random()*(r)%(r-q+1)+q); } re=a/b; shizi=a+" "+fu+" "+b; re1=String.valueOf(re); } } //1乘除法 2除法无余数****1 if(fun==1) { if(c==0) { fu="+"; re=a+b; re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } if(c==1) { fu="-"; //排除负数可能性 while(a-b<0) { a=(int)(Math.random()*(r)%(r-q+1)+q); b=(int)(Math.random()*(r)%(r-q+1)+q); } shizi=a+" "+fu+" "+b; re=a-b; re1=String.valueOf(re); } if(c==2) { fu="×"; re=a*b; re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } if(c==3) { fu="÷"; //排除分母为0的可能性 while(b==0) { b=(int)(Math.random()*(r)%(r-q+1)+q); } re=a/b; if(a%b!=0) { int flag1=a%b; re1=String.valueOf(re)+"..."+String.valueOf(flag1); } else re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } } //1乘除法 2除法无余数****0 if(fun==0) { if(c==0) { fu="+"; re=a+b; re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } if(c==1) { fu="-"; //排除负数可能性 while(a-b<0) { a=(int)(Math.random()*(r)%(r-q+1)+q); b=(int)(Math.random()*(r)%(r-q+1)+q); } re=a-b; re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } if(c==2) { fu="+"; re=a+b; re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } if(c==3) { fu="-"; //排除负数可能性 while(a-b<0) { a=(int)(Math.random()*(r)%(r-q+1)+q); b=(int)(Math.random()*(r)%(r-q+1)+q); } re=a-b; re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } } return shizi; } //***************生成整数公式的函数********************** //***下面是主函数***** public static void main(String[] args) { // TODO 自动生成的方法存根 //连接数据库**************** try { Connection con = null; //定义一个MYSQL链接对象 Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL驱动 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); //链接本地MYSQL java.sql.Statement stmt; //创建声明 stmt = con.createStatement(); int num1,r = 0,q = 0,r1,m=0,fun ; int zhengque = 0;//计算正确的个数(分数) int cuowu = 0;//计算错误的个数(分数) int zhengque1 = 0;//计算正确的个数(整数) int cuowu1 = 0;//计算错误的个数(整数) int zhengque5 = 0;//计算正确的个数(三个整数带括号) int cuowu5 = 0;//计算错误的个数(三个整数带括号) while(m==0) { int flag=0; System.out.print("1:真分数 2:整数 3:三个整数 4:退出 请输入:"); Scanner in2 = new Scanner(System.in); r1 = in2.nextInt(); if(r1==1)//******真分数**** { String correct="",wrong=""; num1=num(); int zhengque2[]=new int[num1+10]; int cuowu2[]=new int[num1+10]; String Array1[]=new String[num1]; String Arr1[]=new String[num1]; System.out.print("请输入算数题中出现的数的最大值:"); Scanner in1 = new Scanner(System.in); r = in1.nextInt(); System.out.println("请输入算数题的控制项(1有乘除法 2没有乘除法)"); System.out.println("例如:1是有乘除法"); System.out.print("请输入控制项:"); Scanner in21 = new Scanner(System.in); fun = in21.nextInt(); while(flag==0) { if(fun==1||fun==2) { for(int i=0;i<num1;i++) { Array1[i]=fun(r,fun); //查重 for(int z=0;z<i;z++) { if((Array1[z].equals(Array1[i]))) { Array1[i]=fun(r,fun); z=0; } } flag=1; System.out.print(i+1+": "+Array1[i]+" = "); //将题目和答案写入数据库表 String sql="INSERT INTO 四则运算题目1 (shizi,result) VALUES ('"+Array1[i]+" = "+"','"+refenshu+"')"; stmt.executeUpdate(sql); //System.out.print("请输入结果:"); String result; Scanner in2223 = new Scanner(System.in); result=in2223.next(); if(result.equals(refenshu)) { System.out.println("恭喜你,回答正确!!!"); zhengque2[zhengque]=i; zhengque++; } else { System.out.println("回答错误!正确答案为:"+refenshu); cuowu2[cuowu]=i; cuowu++; } } } else { System.out.println("输入错误,请重新输入!"); flag=1; } } //输出正确题号和错误题号 System.out.println(); System.out.println("恭喜你,回答正确"+zhengque+"道题!!!"); if(zhengque>0) { System.out.print("正确题号:"); for(int i=0;i<zhengque;i++) { System.out.print(zhengque2[i]+1+" "); int corr=0; corr=zhengque2[i]+1; correct=correct+corr+" "; } } System.out.println(); if(cuowu>0) { System.out.print("错误题号:"); for(int i=0;i<cuowu;i++) { System.out.print(cuowu2[i]+1+" "); int wro=0; wro=cuowu2[i]+1; wrong=wrong+wro+" "; } } //将正确错误的题号写入数据库表 String sql="INSERT INTO Grade (Correct,Wrong) VALUES ('"+zhengque+"("+correct+")','"+cuowu+"("+wrong+")')"; stmt.executeUpdate(sql); correct=""; wrong=""; zhengque=0; cuowu=0; } if(r1==2)//****整数**** { String correct="",wrong=""; num1=num(); int zhengque4[]=new int[num1+10]; int cuowu4[]=new int[num1+10]; String Array[]=new String[num1]; String Arr[]=new String[num1]; //进行最大值最小值的顺序判断*** int w1=0;; while(w1==0) { System.out.print("请输入算数题中出现的数的最大值和最小值:"); Scanner in1 = new Scanner(System.in); r = in1.nextInt(); q = in1.nextInt(); if(r>=q) w1=1; else { w1=0; System.out.println("输入错误!"); } } System.out.println("请输入算数题的控制项(1有乘除法2除法无余数 0都未选择)"); System.out.println("例如:12是有乘除法,除法无余数"); System.out.print("请输入控制项:"); Scanner in211 = new Scanner(System.in); fun = in211.nextInt(); while(flag==0) { if(fun==12||fun==1||fun==0) { for(int i=0;i<num1;i++) { Array[i]=function(r,q,fun); //查重 for(int z=0;z<i;z++) { if((Array[z].equals(Array[i]))) { Array[i]=function(r,q,fun); z=0; } } flag=1; System.out.print(i+1+": "+Array[i]+" = "); //将题目和答案写入数据库表 String sql="INSERT INTO 四则运算题目2 (shizi,result) VALUES ('"+Array[i]+" = "+"','"+re1+"')"; stmt.executeUpdate(sql); //System.out.print("请输入结果:"); String result; Scanner in222 = new Scanner(System.in); result=in222.next(); if(result.equals(re1)) { System.out.println("恭喜你,回答正确!!!"); zhengque4[zhengque1]=i; zhengque1++; } else { System.out.println("回答错误!正确答案为:"+re1); cuowu4[cuowu1]=i; cuowu1++; } } } if(fun!=12&&fun!=1&&fun!=0) { System.out.println("输入错误,请重新输入!"); flag=1; } } System.out.println(); //输出正确题号和错误题号 System.out.println("恭喜你,回答正确"+zhengque1+"道题!!!"); if(zhengque1>0) { System.out.print("正确题号:"); for(int i=0;i<zhengque1;i++) { System.out.print(zhengque4[i]+1+" "); int corr=0; corr=zhengque4[i]+1; correct=correct+corr+" "; } } System.out.println(); if(cuowu1>0) { System.out.print("错误题号:"); for(int i=0;i<cuowu1;i++) { System.out.print(cuowu4[i]+1+" "); int wro=0; wro=cuowu4[i]+1; wrong=wrong+wro+" "; } } //将正确错误的题号写入数据库表 String sql="INSERT INTO Grade (Correct,Wrong) VALUES ('"+zhengque1+"("+correct+")','"+cuowu1+"("+wrong+")')"; stmt.executeUpdate(sql); correct=""; wrong=""; zhengque1=0; cuowu1=0; } if(r1==3)//括号的式子 { String correct="",wrong=""; num1=num(); int zhengque6[]=new int[num1+10]; int cuowu6[]=new int[num1+10]; String Ar[]=new String[num1]; //进行最大值最小值的顺序判断*** int w1=0;; while(w1==0) { System.out.print("请输入算数题中出现的数的最大值和最小值:"); Scanner in1 = new Scanner(System.in); r = in1.nextInt(); q = in1.nextInt(); if(r>=q) w1=1; else { w1=0; System.out.println("输入错误!"); } } for(int i=0;i<num1;i++) { Ar[i]=sgongshi(r,q); //查重 for(int z=0;z<i;z++) { if((Ar[z].equals(Ar[i]))) { Ar[i]=sgongshi(r,q); z=0; } } System.out.print(i+1+": "+Ar[i]+" = "); //将题目和答案写入数据库表 String sql="INSERT INTO 四则运算题目3 (shizi,result) VALUES ('"+Ar[i]+" = "+"','"+resan+"')"; stmt.executeUpdate(sql); //System.out.print("请输入结果:"); String result; Scanner in222 = new Scanner(System.in); result=in222.next(); if(result.equals(String.valueOf(resan))) { System.out.println("恭喜你,回答正确!!!"); zhengque6[zhengque5]=i; zhengque5++; } else { System.out.println("回答错误!正确答案为:"+resan); cuowu6[cuowu5]=i; cuowu5++; } } System.out.println(); //输出正确题号和错误题号 System.out.println("恭喜你,回答正确"+zhengque5+"道题!!!"); if(zhengque5>0) { System.out.print("正确题号:"); for(int i=0;i<zhengque5;i++) { System.out.print(zhengque6[i]+1+" "); int corr=0; corr=zhengque6[i]+1; correct=correct+corr+" "; } } System.out.println(); if(cuowu5>0) { System.out.print("错误题号:"); for(int i=0;i<cuowu5;i++) { System.out.print(cuowu6[i]+1+" "); int wro=0; wro=cuowu6[i]+1; wrong=wrong+wro+" "; } } //将正确错误的题号写入数据库表 String sql="INSERT INTO Grade (Correct,Wrong) VALUES ('"+zhengque5+"("+correct+")','"+cuowu5+"("+wrong+")')"; stmt.executeUpdate(sql); correct=""; wrong=""; zhengque5=0; cuowu5=0; } if(r1==4)//*****退出****** { m=1; System.out.print("谢谢使用!"); } System.out.println(); } } catch (Exception e) { System.out.print("MYSQL ERROR:" + e.getMessage()); } } }
结果截图: