已解决的问题:四则运算包含乘除法,加减有负数。
未解决问题:括号运算未解决,除法余数未解决!
个人总结:学会了把问题分解,把复杂问题简单化!
程序代码:
1 package test; 2 import java.util.*; 3 public class siZe { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 Scanner scan = new Scanner(System.in); 8 int N; 9 int mid; 10 System.out.println("请输入题目的总数:"); 11 N = scan.nextInt(); 12 String[] timu = new String[N];//用于存储题目 13 if(N%2== 0) 14 mid = N/2; 15 else 16 mid = (N-1)/2; 17 for(int i = 0;i<mid;i++){ 18 timu = zhengShu(N); 19 System.out.println(i+1 + ".) "+timu[i]); 20 21 } 22 for(int i = mid;i<N;i++){ 23 timu = zhenFenshu(N); 24 System.out.println(i+1 + ".) "+timu[i]); 25 } 26 27 28 29 30 } 31 32 public static String[] zhengShu(int n){ 33 String[] timu = new String[n]; 34 String linshi = ""; 35 int num1,num2,singal = 0; 36 int i = 0;//统计次数 37 Random random = new Random(); 38 39 while(i < n){ 40 num1 = random.nextInt(100); 41 num2 = random.nextInt(100); 42 singal = random.nextInt(4); 43 44 if(singal == 0) 45 linshi = ""+num1 + "+" + num2 + "=" + num1+num2; 46 if(singal == 1&&num1>num2) 47 linshi = ""+num1 + "-" + num2 + "=" + (num1-num2); 48 if(singal == 2) 49 linshi = ""+num1 + "*" + num2 + "=" + num1*num2; 50 if(singal == 3&&num2!=0&&num1%num2 == 0) 51 linshi = ""+num1 + "/" + num2 + "=" + num1/num2; 52 if(bChongFu(timu,linshi,i)){ 53 timu[i] = linshi; 54 i++; 55 } 56 57 } 58 return timu; 59 60 } 61 62 public static String[] zhenFenshu(int n){ 63 String[] timu = new String[n]; 64 String linshi = ""; 65 int num1,num2,singal, x , y = 0; //num1、num2为分子,a、b为分子 66 int i = 0;//统计次数 67 Random random = new Random(); 68 while(i < n){ 69 num1 = random.nextInt(100); 70 num2 = random.nextInt(100); 71 singal = random.nextInt(4); 72 x = random.nextInt(100); 73 y = random.nextInt(100); 74 75 if(singal == 0 && y!=0 && num1*y+num2*x<=x*y) 76 linshi = ""+num1 + "/" + x +" + "+ num2 + "/" + y + "=" + simpleNumber(num1*y+num2*x,x*y); 77 if(singal == 1 && y!=0 && num1*y+num2*x<=x*y) 78 linshi = ""+num1 + "/" + x +" - "+ num2 + "/" + y + "=" + simpleNumber(num1*y-num2*x,x*y); 79 if(singal == 2 && y!=0 && num1*num2<=x*y) 80 linshi = ""+num1 + "/" + x +" * "+ num2 + "/" + y + "=" +simpleNumber (num1*num2,x*y); 81 if(singal == 3 && y!=0 && num1*y<=num2*x) 82 linshi = ""+num1 + "/" + x + " / "+num2 + "/" + y + "=" + simpleNumber(num1*y,num2*x); 83 if(bChongFu(timu,linshi,i)){ 84 timu[i] = linshi; 85 i++; 86 } 87 } 88 return timu; 89 90 } 91 92 93 public static String simpleNumber(int f1,int f2){ 94 int maxYs = 1; 95 for(int i = f1;i>=1;i--){ 96 if(f1%i==0 && f2%i==0){ 97 maxYs = i; 98 break; 99 } 100 } 101 f1 = f1 / maxYs; 102 f2 = f2 / maxYs; 103 if(f1 == 0) 104 return "0"; 105 else 106 return +f1+"/"+f2; 107 } 108 109 public static boolean bChongFu(String[] timu,String linshi,int n){ 110 for(int i = 0;i<n;i++){ 111 if(timu[i].equals(linshi)) 112 return false; 113 } 114 return true; 115 116 } 117 118 119 }
实验截图: