设计思想:之前的算法是只进行算式的输出,不用进行运算,所以我把所有的算式和符号转化成了string类型,但是如果要进行判断打分,就要在int阶段进行计算。进行讨论之后,我们找到一个方法去解决长算式如何计算,首先要算乘除法,找到随机出来的运算符,如果是乘除,则优先运算,运算后把数存进新数组,然后进行加减计算。
import java.util.Random; import java.io.*; public class Yunsuan01 {//判断出题是否重复 int chongfu(int numtishu,String result[],int i) { for(int j=0;j<i;j++) { if(result[i] == result[i-j-1]); i = i-1; } return i; } int weishu(int judgeweishu)//随机位数 { int numsuiji = 0; Random ran = new Random();//初始化随机数 if(judgeweishu == 0) { numsuiji = ran.nextInt(10); } else if(judgeweishu == 1) { numsuiji = ran.nextInt(100); } else if(judgeweishu == 2) { numsuiji = ran.nextInt(1000); } else if(judgeweishu == 3) { numsuiji = ran.nextInt(10000); } return numsuiji; } char fuhao(int judgechengchu) { char fuhao = ' '; Random ran = new Random();//初始化随机数 int suijijiajian = ran.nextInt(2); int suijichengchu = ran.nextInt(4); if(judgechengchu == 0) { if(suijijiajian == 0) fuhao = '+'; if(suijijiajian == 1) fuhao = '-'; } if(judgechengchu == 1) { if(suijichengchu == 0) fuhao = '+'; if(suijichengchu == 1) fuhao = '-'; if(suijichengchu == 2) fuhao = '*'; if(suijichengchu == 3) fuhao = '/'; } return fuhao ; } void dayinfangshi(int judgefangshi,String last) throws FileNotFoundException { if(judgefangshi == 1) { FileOutputStream fs = new FileOutputStream(new File("D:\四则运算.txt")); PrintStream p = new PrintStream(fs); p.println(last); } if(judgefangshi == 0) System.out.println(last); } int getresult(int num[],char chars[],int numchangdu) { int result = num[0]; /*int mark=0; for(int i=0 ;i< numchangdu-1;i++)//记录有多少个乘除 { if(chars[i] == '*'||chars[i] == '/') { mark += 1; } } for(int m=0;m<mark;m++)//有多少个乘除就进行多少次运算 { for(int i=0 ;i< numchangdu-1;i++)//找到第一个乘或除(按符号个数循环) { if(chars[i] == '*') { num[i] *= num[i+1]; for(int j=i;j<numchangdu-1-m;j++)//乘运算之后向前移动后面的数 { num[j+1-m] = num[j+2-m]; } for(int j=i;j<numchangdu-1-m;j++) { chars[j-m] = chars[j+1-m]; } break; } if(chars[i] == '/') { num[i] /= num[i+1]; for(int j=i;j<numchangdu-1-m;j++)//除运算之后向前移动后面的数 { num[j+1-m] = num[j+2-m]; } for(int j=i;j<numchangdu-1-m;j++) { chars[j] = chars[j+1]; } break; } } } result = num[0]; for(int m=0;m<numchangdu-mark;m++) { if(chars[m] == '+') result += num[m+1]; if(chars[m] == '-') result -= num[m+1]; }*/ for(int i=0 ;i< numchangdu-1;i++) { if(chars[i] == '+') result += num[i+1]; if(chars[i] == '-') result -= num[i+1]; } return result; } }
import java.io.FileNotFoundException; import java.util.*;//引用util包 import java.util.Random; public class Yunsuan { public static void main(String[] args) throws FileNotFoundException { // TODO Auto-generated method stub int numtishu; int numchangdu; int judgezhengfen; int judgechengchu; int judgekuohao; int judgeyushu = 0; int judgeweishu = 0; int judgefangshi = 0; String last = new String(); Scanner sca = new Scanner(System.in); Random ran = new Random();//初始化随机数 Yunsuan01 y = new Yunsuan01(); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~判断类型~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ while(true){ System.out.println("真分数运算(1),整数运算(0):"); judgezhengfen = sca.nextInt(); if(judgezhengfen!=1 && judgezhengfen!=0) { System.out.println("输入有误,请重新输入"); } if(judgezhengfen == 0) { while(true) { System.out.println("位数:四位数(3),三位数(2),两位数(1),一位数(0):"); judgeweishu = sca.nextInt(); if(judgeweishu!=1 && judgeweishu!=0 && judgeweishu!=2&&judgeweishu!=3) { System.out.println("输入有误,请重新输入"); } else break; } break; } else break; } while(true){ System.out.println("有乘除法(1),无乘除法(0)"); judgechengchu = sca.nextInt(); if(judgechengchu!=1 && judgechengchu!=0) { System.out.println("输入有误,请重新输入"); } if(judgechengchu == 1) { while(true) { System.out.println("除法有余数(1),无余数(0)"); judgeyushu = sca.nextInt(); if(judgeyushu!=1 && judgeyushu!=0) { System.out.println("输入有误,请重新输入"); } else break; } break; } else break; } while(true){ System.out.println("有括号(1),无括号(0)"); judgekuohao = sca.nextInt(); if(judgekuohao!=1 && judgekuohao!=0) { System.out.println("输入有误,请重新输入"); } else break; } while(true) { System.out.println("输入有几个数参与运算(2~10):"); numchangdu = sca.nextInt();//输入题的长度 if(numchangdu<2 || numchangdu>10) { System.out.println("输入有误,请重新"); } else break; } System.out.println("输入题目的数量:"); numtishu = sca.nextInt();//输入题数 String result[] = new String[numtishu]; for(int i=0; i<numtishu;i++) { result[i]=""; } while(true){ System.out.println("打印方式:txt格式(1),控制台(0):"); judgefangshi = sca.nextInt(); if(judgekuohao!=1 && judgekuohao!=0) { System.out.println("输入有误,请重新输入"); } else break; } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~给出算式~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ if(judgezhengfen == 0 && judgekuohao == 0){ //整数,无括号 for(int i=0; i<numtishu; i++) { int k = i+1; int num[] = new int[numchangdu]; char chars[] = new char[numchangdu-1]; result[i] = result[i]+"["+k+"]"; for(int j=0;j<numchangdu;j++) { num[j] = y.weishu(judgeweishu);//按位数出随机数 } for(int j=0;j<numchangdu-1;j++) { chars[j] = y.fuhao(judgechengchu);//按是否乘除出符号 } for(int j=0;j<numchangdu-1;j++) { if(chars[j] == '-')//减法不出现负数 { //System.out.println("~~~~~~~~~~ "); //测试是否执行 if(num[j]<num[j+1]) { int temp = num[j]; num[j] = num[j+1]; num[j+1] = temp; } } } if(judgekuohao == 1) { System.out.println("~~~~");//测验是否随机出数 int rankuohao; for(int j=0;j<numchangdu-1;j++) { rankuohao = ran.nextInt(2); System.out.println(rankuohao);//测验是否随机出数 if(rankuohao == 1){ result[i] = result[i]+"("+num[j]+chars[j]; } if(rankuohao == 0) result[i] = result[i]+num[j]+chars[j]; } result[i] += num[numchangdu-1]; result[i] += "="; } if(judgekuohao == 0){ for(int j=0;j<numchangdu-1;j++) { result[i] = result[i]+num[j]+chars[j]; } result[i] += num[numchangdu-1]; result[i] += "= "; } System.out.println(result[i]); int answer = sca.nextInt(); if(answer == y.getresult(num, chars, numchangdu)) { System.out.println("√"); } if(answer != y.getresult(num, chars, numchangdu)) { System.out.println("×"); } } /*for(int j = 0; j<numtishu;j++) { last = last + result[j]; } y.dayinfangshi(judgefangshi, last);//最终输出*/ } else if(judgezhengfen == 1) { //整数,有乘除法,无括号,有余数 } } }
结果:
表格
:
照片: