软件工程个人作业01
1.设计思想
① 先设定几个随机数来分别代表第一个和第二个分数的分子和分母,在设定两个整型变量来存放结果的分子和分母,在设定一个计数器来记录次数,最高到30,再设定一个随机数从0-3,分别代表四种运算符号;
② 之后判断两个分母,若有0或不能成为真分数,则跳过此次循环,重新随机,否则则进行下一步骤;
③ 判断运算符号,用if语句来决定不同的计算方法,并且输出。
④ 等到进行完30次循环后,程序结束。
2.源程序代码
1 public class PrimaryCalculate 2 { 3 public static void main(String args[]) 4 { 5 for(int time=1;time<=30;) 6 { 7 int one=(int)(Math.random()*101); 8 int two=(int)(Math.random()*101); 9 int three=(int)(Math.random()*101); 10 int four=(int)(Math.random()*101); 11 int symbol=(int)(Math.random()*4); 12 int five; 13 int six; 14 15 if(two==0||four==0||one>two||three>four) 16 { 17 continue; 18 } 19 20 if(symbol==0) 21 { 22 if(two>four) 23 { 24 for(int i=two;;i++) 25 { 26 if(i%two==0&&i%four==0) 27 { 28 six=i; 29 one=six/two+one; 30 three=six/four+three; 31 break; 32 } 33 } 34 } 35 else if(two<four) 36 { 37 for(int i=four;;i++) 38 { 39 if(i%two==0&&i%four==0) 40 { 41 six=i; 42 one=six/two+one; 43 three=six/four+three; 44 break; 45 } 46 } 47 } 48 else 49 { 50 six=two; 51 } 52 53 five=one+three; 54 55 if(five==0) 56 { 57 System.out.println(one+"/"+two+" + "+three+"/"+four+" = "+"0"); 58 } 59 else if(five>six) 60 { 61 for(int j=six;;j--) 62 { 63 if(five%j==0&&six%j==0) 64 { 65 five=five/j; 66 six=six/j; 67 break; 68 } 69 } 70 System.out.println(one+"/"+two+" + "+three+"/"+four+" = "+five+"/"+six); 71 } 72 else if(five<six) 73 { 74 for(int j=five;;j--) 75 { 76 if(five%j==0&&six%j==0) 77 { 78 five=five/j; 79 six=six/j; 80 break; 81 } 82 } 83 System.out.println(one+"/"+two+" + "+three+"/"+four+" = "+five+"/"+six); 84 } 85 } 86 else if(symbol==1) 87 { 88 if(two>four) 89 { 90 for(int i=two;;i++) 91 { 92 if(i%two==0&&i%four==0) 93 { 94 six=i; 95 one=six/two+one; 96 three=six/four+three; 97 break; 98 } 99 } 100 } 101 else if(two<four) 102 { 103 for(int i=four;;i++) 104 { 105 if(i%two==0&&i%four==0) 106 { 107 six=i; 108 one=six/two+one; 109 three=six/four+three; 110 break; 111 } 112 } 113 } 114 else 115 { 116 six=two; 117 } 118 119 five=one-three; 120 121 if(five==0) 122 { 123 System.out.println(one+"/"+two+" - "+three+"/"+four+" = "+"0"); 124 } 125 else if(five>six) 126 { 127 for(int j=six;;j--) 128 { 129 if(five%j==0&&six%j==0) 130 { 131 five=five/j; 132 six=six/j; 133 break; 134 } 135 } 136 System.out.println(one+"/"+two+" - "+three+"/"+four+" = "+five+"/"+six); 137 } 138 else if(five<six) 139 { 140 for(int j=five;;j--) 141 { 142 if(five%j==0&&six%j==0) 143 { 144 five=five/j; 145 six=six/j; 146 break; 147 } 148 } 149 System.out.println(one+"/"+two+" - "+three+"/"+four+" = "+five+"/"+six); 150 } 151 } 152 else if(symbol==2) 153 { 154 five=one*three; 155 six=two*four; 156 157 if(five==0) 158 { 159 System.out.println(one+"/"+two+" * "+three+"/"+four+" = "+"0"); 160 } 161 else if(five>six) 162 { 163 for(int j=six;;j--) 164 { 165 if(five%j==0&&six%j==0) 166 { 167 five=five/j; 168 six=six/j; 169 break; 170 } 171 } 172 System.out.println(one+"/"+two+" * "+three+"/"+four+" = "+five+"/"+six); 173 } 174 else if(five<six) 175 { 176 for(int j=five;;j--) 177 { 178 if(five%j==0&&six%j==0) 179 { 180 five=five/j; 181 six=six/j; 182 break; 183 } 184 } 185 System.out.println(one+"/"+two+" * "+three+"/"+four+" = "+five+"/"+six); 186 } 187 } 188 else if(symbol==3) 189 { 190 five=one*four; 191 six=two*three; 192 if(five==0) 193 { 194 System.out.println(one+"/"+two+" / "+three+"/"+four+" = "+"0"); 195 } 196 else if(five>six) 197 { 198 for(int j=six;;j--) 199 { 200 if(five%j==0&&six%j==0) 201 { 202 five=five/j; 203 six=six/j; 204 break; 205 } 206 } 207 System.out.println(one+"/"+two+" / "+three+"/"+four+" = "+five+"/"+six); 208 } 209 else if(five<six) 210 { 211 for(int j=five;;j--) 212 { 213 if(five%j==0&&six%j==0) 214 { 215 five=five/j; 216 six=six/j; 217 break; 218 } 219 } 220 System.out.println(one+"/"+two+" / "+three+"/"+four+" = "+five+"/"+six); 221 } 222 } 223 224 time++; 225 226 } 227 } 228 }
3.运行结果截图
4.上课未按时完成的原因:未带电脑