个人PSP(四则运算)
-
题目要求:能自动生成小学四则运算题目,且每一道题目的运算结果不能为负。除了支持整数运算外,还要支持真分数四则运算。
-
工具清单:java代码:
- 代码
1 package ForArithmetic; 2 import java.util.Scanner; 3 import java.util.Random; 4 import static java.lang.Math.*; 5 public class ForArithmetic{ 6 public static void main(String[] args) { 7 Scanner scanner = new Scanner(System.in); 8 System.out.println("请输入数字上限:"); 9 int num = scanner.nextInt(); 10 System.out.println("请输入题目数量:"); 11 int t = scanner.nextInt(); 12 SizeYunsuan subject = new SizeYunsuan(); 13 subject.set(num, t); 14 15 } 16 } 17 class SizeYunsuan { 18 void set (int num , int t){ 19 Random random = new Random(); 20 Scanner scanner = new Scanner(System.in); 21 JiSuan j = new JiSuan(); 22 23 String arithmetic[] = new String[t]; 24 for(int i = 0 ; i < t ; i++) { 25 int a = (int)random.nextInt(num);//分子 26 int b = (int)random.nextInt(num);//分母 27 int c = (int)random.nextInt(num);//另一个分子 28 int d = (int)random.nextInt(num);//另一个分母 29 int fuhao = random.nextInt(4);//0代表+,1代表-,2代表乘,3代表除 30 if(b!=0 && d!=0) { 31 if(fuhao==0) { 32 int fenzi = a*d + b*c; 33 int fenmu = b*d; 34 arithmetic[i] = biaodashi(a,b) + '+' + biaodashi(c,d) + '='; 35 System.out.println((i+1)+"、"+arithmetic[i]); 36 j.anw(fenzi, fenmu); 37 38 } 39 if(fuhao==1 && a*d - b*c >= 0) { 40 int fenzi = a*d - b*c; 41 int fenmu = b*d; 42 arithmetic[i] = biaodashi(a,b) + '-' + biaodashi(c,d) + '='; 43 System.out.println((i+1)+"、"+arithmetic[i]); 44 j.anw(fenzi, fenmu); 45 } 46 if(fuhao==1 && a*d - b*c < 0) { 47 int fenzi = b*c - a*d; 48 int fenmu = b*d; 49 arithmetic[i] = biaodashi(c, d) + '-' + biaodashi(a, b) + '='; 50 System.out.println((i+1)+"、"+arithmetic[i]); 51 j.anw(fenzi, fenmu); 52 } 53 if(fuhao==2) { 54 int fenzi = a*c; 55 int fenmu = b*d; 56 arithmetic[i] = biaodashi(a, b) + '×' + biaodashi(c, d) + '='; 57 System.out.println((i+1)+"、"+arithmetic[i]); 58 j.anw(fenzi, fenmu); 59 } 60 if(fuhao==3 && c!=0) { 61 int fenzi = a*d; 62 int fenmu = b*c; 63 arithmetic[i] = biaodashi(a, b) + '÷' + biaodashi(c, d) + '='; 64 System.out.println((i+1)+"、"+arithmetic[i]); 65 j.anw(fenzi, fenmu); 66 } 67 if(fuhao==3 && c==0) { 68 i--; 69 } 70 } 71 else { 72 b=1; 73 d=1; 74 if(fuhao==0) { 75 int fenzi = a*d + b*c; 76 int fenmu = b*d; 77 arithmetic[i] = a + "+" +c + "="; 78 System.out.println((i+1)+"、"+arithmetic[i]); 79 j.anw(fenzi, fenmu); 80 } 81 if(fuhao==1 && a*d - b*c >= 0) { 82 int fenzi = a*d - b*c; 83 int fenmu = b*d; 84 arithmetic[i] = a + "-" +c + "="; 85 System.out.println((i+1)+"、"+arithmetic[i]); 86 j.anw(fenzi, fenmu); 87 } 88 if(fuhao==1 && a*d - b*c < 0) { 89 int fenzi = b*c - a*d; 90 int fenmu = b*d; 91 arithmetic[i] = c + "-" +a + "="; 92 System.out.println((i+1)+"、"+arithmetic[i]); 93 j.anw(fenzi, fenmu); 94 } 95 if(fuhao==2) { 96 int fenzi = a*c ; 97 int fenmu = b*d; 98 arithmetic[i] = a + "×" +c + "="; 99 System.out.println((i+1)+"、"+arithmetic[i]); 100 j.anw(fenzi, fenmu); 101 } 102 if(fuhao==3 && c!=0) { 103 int fenzi = a*d ; 104 int fenmu = b*c; 105 arithmetic[i] = a + "÷" +c + "="; 106 System.out.println((i+1)+"、"+arithmetic[i]); 107 j.anw(fenzi, fenmu); 108 } 109 if(fuhao==3 && c==0) { 110 i--; 111 } 112 113 } 114 } 115 } 116 public static String biaodashi(int a,int b) {//判断假分数,并化假分数为带分数 117 if(a>=b) { 118 int c; 119 c = a/b; 120 int d; 121 d = a%b ; 122 if(d==0) 123 return c+""; 124 else 125 return a+"/"+b; 126 } 127 else { 128 return a+"/"+b; 129 } 130 } 131 } 132 133 134 class JiSuan { 135 void anw(int fenzi,int fenmu){ 136 Scanner scanner = new Scanner(System.in); 137 double d = (double)fenzi/fenmu; 138 double sum = ((Math.round(d * 100)) / 100.0); 139 String anw = scanner.nextLine(); 140 String string[] =anw.split("/"); 141 double [] result =new double[string.length]; 142 for(int i=0;i<string.length ;i++) { 143 result[i]=Double.parseDouble(string[i]); 144 } 145 double sum1 =0; 146 if(string.length == 1 ) { 147 sum1 = (int) result[0]; 148 } 149 else { 150 sum1 = result[0]/result[1]; 151 } 152 153 if(sum==sum1) 154 System.out.println("正确"); 155 else { 156 System.out.println("错误"); 157 System.out.println("正确答案为:"+sum); 158 } 159 160 } 161 }
4.代码运行结果:
5.PSP表格: