1、产生随机的四位数,如果出现0则把0赋值为1
1 int rightNum = 0; //正确题数 2 int wrongNum = 0; //错误题数 3 Scanner scan = new Scanner(System.in); 4 for(int i =1;i<=10;i++) { 5 System.out.println("第"+ i +"题"); 6 int num1 = (int )(Math.random()*10); 7 int num2 = (int )(Math.random()*10); 8 int num3 = (int )(Math.random()*10); 9 int num4 = (int )(Math.random()*10); 10 if(num1 == 0) { 11 num1 = 1; 12 }if(num2 == 0) { 13 num2 = 1; 14 }if(num3 == 0) { 15 num3 = 1; 16 }if(num4 == 0) { 17 num4 = 1; 18 }
2、产生随机的3个运算符
1 Random num = new Random();// 生成随机运算符 2 int index = num.nextInt(3) + 1; 3 char[] fuhaos = { ‘+‘, ‘-‘, ‘*‘, ‘/‘ }; 4 String fuhao1 = String.valueOf(fuhaos[index]); 5 String fuhao2 = String.valueOf(fuhaos[index]); 6 String fuhao3 = String.valueOf(fuhaos[index]);
3、判断每个符号,fuhao1代表第一个位置符号,fuhao2代表第二个位置符号,fuhao3代表第三个位置符号
1 if(fuhao1.equals("+")) { 2 if(fuhao2.equals("+")){ 3 if(fuhao3.equals("+")) { //最后一位符号 4 System.out.println(num1 +"+"+num2 +"+"+num3 +"+"+num4+"=");// +++ 5 double sum = scan.nextDouble(); 6 if(sum == (num1+num2+num3+num4)){ 7 System.out.println("回答正确"); 8 rightNum++; 9 }else{ 10 System.out.println("回答错误"); 11 wrongNum++; 12 } 13
4、对每一个符号位进行判断,显示第一个符号位为加号时
1 if(fuhao1.equals("+")) { 2 if(fuhao2.equals("+")){ 3 if(fuhao3.equals("+")) { //最后一位符号 4 System.out.println(num1 +"+"+num2 +"+"+num3 +"+"+num4+"=");// +++ 5 double sum = scan.nextDouble(); 6 if(sum == (num1+num2+num3+num4)){ 7 System.out.println("回答正确"); 8 rightNum++; 9 }else{ 10 System.out.println("回答错误"); 11 wrongNum++; 12 } 13 14 }else if(fuhao3.equals("-")){ //++- 15 System.out.println(num1 +"+"+num2 +"+"+num3 +"-"+num4+"="); 16 double sum = scan.nextDouble(); 17 if(sum == (num1+num2+num3-num4)){ 18 System.out.println("回答正确"); 19 rightNum++; 20 }else{ 21 System.out.println("回答错误"); 22 wrongNum++; 23 } 24 }else if(fuhao3.equals("*")){ //++* 25 System.out.println(num1 +"+"+num2 +"+"+num3 +"*"+num4+"="); 26 double sum = scan.nextDouble(); 27 if(sum == (num1+num2+num3*num4)){ 28 System.out.println("回答正确"); 29 rightNum++; 30 }else{ 31 System.out.println("回答错误"); 32 wrongNum++; 33 } 34 }else{ //++/ 35 System.out.println(num1 +"+"+num2 +"+"+num3 +"/"+num4+"="); 36 double sum = scan.nextDouble(); 37 if(sum == (num1+num2+num3/num4)){ 38 System.out.println("回答正确"); 39 rightNum++; 40 }else{ 41 System.out.println("回答错误"); 42 wrongNum++; 43 } 44 45 } //第二位符号位结束 46 }else if (fuhao2.equals("-")){ //+-+ 47 if(fuhao3.equals("+")) { 48 System.out.println(num1 +"+"+num2 +"-"+num3 +"+"+num4+"="); 49 double sum = scan.nextDouble(); 50 if(sum == (num1-num2-num3+num4)){ 51 System.out.println("回答正确"); 52 rightNum++; 53 }else{ 54 System.out.println("回答错误"); 55 wrongNum++; 56 } 57 58 }else if(fuhao3.equals("-")){ //+-- 59 System.out.println(num1 +"+"+num2 +"-"+num3 +"-"+num4+"="); 60 double sum = scan.nextDouble(); 61 if(sum == (num1+num2-num3-num4)){ 62 System.out.println("回答正确"); 63 rightNum++; 64 }else{ 65 System.out.println("回答错误"); 66 wrongNum++; 67 } 68 }else if(fuhao3.equals("*")){ //+-* 69 System.out.println(num1 +"+"+num2 +"-"+num3 +"*"+num4+"="); 70 double sum = scan.nextDouble(); 71 if(sum == (num1+num2-num3*num4)){ 72 System.out.println("回答正确"); 73 rightNum++; 74 }else{ 75 System.out.println("回答错误"); 76 wrongNum++; 77 } 78 }else{ //+-/ 79 System.out.println(num1 +"+"+num2 +"-"+num3 +"/"+num4+"="); 80 double sum = scan.nextDouble(); 81 if(sum == (num1+num2-num3/num4)){ 82 System.out.println("回答正确"); 83 rightNum++; 84 }else{ 85 System.out.println("回答错误"); 86 wrongNum++; 87 } 88 89 } 90 91 }else if (fuhao2.equals("*")){ //+*+ 92 if(fuhao3.equals("+")) { 93 System.out.println(num1 +"+"+num2 +"*"+num3 +"+"+num4+"="); 94 double sum = scan.nextDouble(); 95 if(sum == (num1+num2*num3+num4)){ 96 System.out.println("回答正确"); 97 rightNum++; 98 }else{ 99 System.out.println("回答错误"); 100 wrongNum++; 101 } 102 103 }else if(fuhao3.equals("-")){ //+*- 104 System.out.println(num1 +"+"+num2 +"*"+num3 +"-"+num4+"="); 105 double sum = scan.nextDouble(); 106 if(sum == (num1+num2*num3-num4)){ 107 System.out.println("回答正确"); 108 rightNum++; 109 }else{ 110 System.out.println("回答错误"); 111 wrongNum++; 112 } 113 }else if(fuhao3.equals("*")){ //+** 114 System.out.println(num1 +"+"+num2 +"*"+num3 +"*"+num4+"="); 115 double sum = scan.nextDouble(); 116 if(sum == (num1+num2*num3*num4)){ 117 System.out.println("回答正确"); 118 rightNum++; 119 }else{ 120 System.out.println("回答错误"); 121 wrongNum++; 122 } 123 }else{ //+*/ 124 System.out.println(num1 +"+"+num2 +"*"+num3 +"/"+num4+"="); 125 double sum = scan.nextDouble(); 126 if(sum == (num1+num2*num3/num4)){ 127 System.out.println("回答正确"); 128 rightNum++; 129 }else{ 130 System.out.println("回答错误"); 131 wrongNum++; 132 } 133 } 134 }else{ //+/+ 135 if(fuhao3.equals("+")) { 136 System.out.println(num1 +"+"+num2 +"/"+num3 +"+"+num4+"="); 137 double sum = scan.nextDouble(); 138 if(sum == (num1+num2/num3+num4)){ 139 System.out.println("回答正确"); 140 rightNum++; 141 }else{ 142 System.out.println("回答错误"); 143 wrongNum++; 144 } 145 146 }else if(fuhao3.equals("-")){ //+/- 147 System.out.println(num1 +"+"+num2 +"/"+num3 +"-"+num4+"="); 148 double sum = scan.nextDouble(); 149 if(sum == (num1+num2/num3-num4)){ 150 System.out.println("回答正确"); 151 rightNum++; 152 }else{ 153 System.out.println("回答错误"); 154 wrongNum++; 155 } 156 }else if(fuhao3.equals("*")){ //+/* 157 System.out.println(num1 +"+"+num2 +"/"+num3 +"*"+num4+"="); 158 double sum = scan.nextDouble(); 159 if(sum == (num1+num2/num3*num4)){ 160 System.out.println("回答正确"); 161 rightNum++; 162 }else{ 163 System.out.println("回答错误"); 164 wrongNum++; 165 } 166 }else{ //+// 167 System.out.println(num1 +"+"+num2 +"/"+num3 +"/"+num4+"="); 168 double sum = scan.nextDouble(); 169 if(sum == (num1+num2/num3/num4)){ 170 System.out.println("回答正确"); 171 rightNum++; 172 }else{ 173 System.out.println("回答错误"); 174 wrongNum++; 175 } 176 } 177 }
5、第一个符号位为减号时
1 else if(fuhao1.equals("-")) { //第一位减号开始 2 if(fuhao2.equals("+")){ 3 if(fuhao3.equals("+")) { 4 System.out.println(num1 +"-"+num2 +"+"+num3 +"+"+num4+"=");// -++ 5 double sum = scan.nextDouble(); 6 if(sum == (num1-num2+num3+num4)){ 7 System.out.println("回答正确"); 8 rightNum++; 9 }else{ 10 System.out.println("回答错误"); 11 wrongNum++; 12 } 13 14 }else if(fuhao3.equals("-")){ //-+- 15 System.out.println(num1 +"-"+num2 +"+"+num3 +"-"+num4+"="); 16 double sum = scan.nextDouble(); 17 if(sum == (num1-num2+num3-num4)){ 18 System.out.println("回答正确"); 19 rightNum++; 20 }else{ 21 System.out.println("回答错误"); 22 wrongNum++; 23 } 24 }else if(fuhao3.equals("*")){ //-+* 25 System.out.println(num1 +"-"+num2 +"+"+num3 +"*"+num4+"="); 26 double sum = scan.nextDouble(); 27 if(sum == (num1-num2+num3*num4)){ 28 System.out.println("回答正确"); 29 rightNum++; 30 }else{ 31 System.out.println("回答错误"); 32 wrongNum++; 33 } 34 }else{ //-+/ 35 System.out.println(num1 +"-"+num2 +"+"+num3 +"/"+num4+"="); 36 double sum = scan.nextDouble(); 37 if(sum == (num1-num2+num3/num4)){ 38 System.out.println("回答正确"); 39 rightNum++; 40 }else{ 41 System.out.println("回答错误"); 42 wrongNum++; 43 } 44 45 } 46 }else if (fuhao2.equals("-")){ //--+ 47 if(fuhao3.equals("+")) { 48 System.out.println(num1 +"-"+num2 +"-"+num3 +"+"+num4+"="); 49 double sum = scan.nextDouble(); 50 if(sum == (num1-num2-num3+num4)){ 51 System.out.println("回答正确"); 52 rightNum++; 53 }else{ 54 System.out.println("回答错误"); 55 wrongNum++; 56 } 57 58 }else if(fuhao3.equals("-")){ //--- 59 System.out.println(num1 +"-"+num2 +"-"+num3 +"-"+num4+"="); 60 double sum = scan.nextDouble(); 61 if(sum == (num1-num2-num3-num4)){ 62 System.out.println("回答正确"); 63 rightNum++; 64 }else{ 65 System.out.println("回答错误"); 66 wrongNum++; 67 } 68 }else if(fuhao3.equals("*")){ //--* 69 System.out.println(num1 +"-"+num2 +"-"+num3 +"*"+num4+"="); 70 double sum = scan.nextDouble(); 71 if(sum == (num1-num2-num3*num4)){ 72 System.out.println("回答正确"); 73 rightNum++; 74 }else{ 75 System.out.println("回答错误"); 76 wrongNum++; 77 } 78 }else{ //--/ 79 System.out.println(num1 +"-"+num2 +"-"+num3 +"/"+num4+"="); 80 double sum = scan.nextDouble(); 81 if(sum == (num1-num2-num3/num4)){ 82 System.out.println("回答正确"); 83 rightNum++; 84 }else{ 85 System.out.println("回答错误"); 86 wrongNum++; 87 } 88 89 } 90 91 }else if (fuhao2.equals("*")){ //-*+ 92 if(fuhao3.equals("+")) { 93 System.out.println(num1 +"-"+num2 +"*"+num3 +"+"+num4+"="); 94 double sum = scan.nextDouble(); 95 if(sum == (num1-num2*num3+num4)){ 96 System.out.println("回答正确"); 97 rightNum++; 98 }else{ 99 System.out.println("回答错误"); 100 wrongNum++; 101 } 102 103 }else if(fuhao3.equals("-")){ //-*- 104 System.out.println(num1 +"-"+num2 +"*"+num3 +"-"+num4+"="); 105 double sum = scan.nextDouble(); 106 if(sum == (num1-num2*num3-num4)){ 107 System.out.println("回答正确"); 108 rightNum++; 109 }else{ 110 System.out.println("回答错误"); 111 wrongNum++; 112 } 113 }else if(fuhao3.equals("*")){ //-** 114 System.out.println(num1 +"-"+num2 +"*"+num3 +"*"+num4+"="); 115 double sum = scan.nextDouble(); 116 if(sum == (num1-num2*num3*num4)){ 117 System.out.println("回答正确"); 118 rightNum++; 119 }else{ 120 System.out.println("回答错误"); 121 wrongNum++; 122 } 123 }else{ //-*/ 124 System.out.println(num1 +"-"+num2 +"*"+num3 +"/"+num4+"="); 125 double sum = scan.nextDouble(); 126 if(sum == (num1-num2*num3/num4)){ 127 System.out.println("回答正确"); 128 rightNum++; 129 }else{ 130 System.out.println("回答错误"); 131 wrongNum++; 132 } 133 } 134 }else{ //-/+ 135 if(fuhao3.equals("+")) { 136 System.out.println(num1 +"-"+num2 +"/"+num3 +"+"+num4+"="); 137 double sum = scan.nextDouble(); 138 if(sum == (num1-num2/num3+num4)){ 139 System.out.println("回答正确"); 140 rightNum++; 141 }else{ 142 System.out.println("回答错误"); 143 wrongNum++; 144 } 145 146 }else if(fuhao3.equals("-")){ //-/- 147 System.out.println(num1 +"-"+num2 +"/"+num3 +"-"+num4+"="); 148 double sum = scan.nextDouble(); 149 if(sum == (num1-num2/num3-num4)){ 150 System.out.println("回答正确"); 151 rightNum++; 152 }else{ 153 System.out.println("回答错误"); 154 wrongNum++; 155 } 156 }else if(fuhao3.equals("*")){ //-/* 157 System.out.println(num1 +"-"+num2 +"/"+num3 +"*"+num4+"="); 158 double sum = scan.nextDouble(); 159 if(sum == (num1-num2/num3*num4)){ 160 System.out.println("回答正确"); 161 rightNum++; 162 }else{ 163 System.out.println("回答错误"); 164 wrongNum++; 165 } 166 }else{ //-// 167 System.out.println(num1 +"-"+num2 +"/"+num3 +"/"+num4+"="); 168 double sum = scan.nextDouble(); 169 if(sum == (num1-num2/num3/num4)){ 170 System.out.println("回答正确"); 171 rightNum++; 172 }else{ 173 System.out.println("回答错误"); 174 wrongNum++; 175 } 176 } 177 }
6、第一个符号位乘号时
1 else if(fuhao1.equals("*")){ //第一位乘法开始 2 if(fuhao2.equals("+")){ 3 if(fuhao3.equals("+")) { //最后一位符号 4 System.out.println(num1 +"+"+num2 +"+"+num3 +"+"+num4+"=");// *++ 5 double sum = scan.nextDouble(); 6 if(sum == (num1*num2+num3+num4)){ 7 System.out.println("回答正确"); 8 rightNum++; 9 }else{ 10 System.out.println("回答错误"); 11 wrongNum++; 12 } 13 14 }else if(fuhao3.equals("-")){ //*+- 15 System.out.println(num1 +"*"+num2 +"+"+num3 +"-"+num4+"="); 16 double sum = scan.nextDouble(); 17 if(sum == (num1*num2+num3-num4)){ 18 System.out.println("回答正确"); 19 rightNum++; 20 }else{ 21 System.out.println("回答错误"); 22 wrongNum++; 23 } 24 }else if(fuhao3.equals("*")){ //*+* 25 System.out.println(num1 +"*"+num2 +"+"+num3 +"*"+num4+"="); 26 double sum = scan.nextDouble(); 27 if(sum == (num1*num2+num3*num4)){ 28 System.out.println("回答正确"); 29 rightNum++; 30 }else{ 31 System.out.println("回答错误"); 32 wrongNum++; 33 } 34 }else{ //*+/ 35 System.out.println(num1 +"*"+num2 +"+"+num3 +"/"+num4+"="); 36 double sum = scan.nextDouble(); 37 if(sum == (num1*num2+num3/num4)){ 38 System.out.println("回答正确"); 39 rightNum++; 40 }else{ 41 System.out.println("回答错误"); 42 wrongNum++; 43 } 44 45 } 46 }else if (fuhao2.equals("-")){ //*-+ 47 if(fuhao3.equals("+")) { 48 System.out.println(num1 +"*"+num2 +"-"+num3 +"+"+num4+"="); 49 double sum = scan.nextDouble(); 50 if(sum == (num1*num2-num3+num4)){ 51 System.out.println("回答正确"); 52 rightNum++; 53 }else{ 54 System.out.println("回答错误"); 55 wrongNum++; 56 } 57 58 }else if(fuhao3.equals("-")){ //*-- 59 System.out.println(num1 +"*"+num2 +"-"+num3 +"-"+num4+"="); 60 double sum = scan.nextDouble(); 61 if(sum == (num1*num2-num3-num4)){ 62 System.out.println("回答正确"); 63 rightNum++; 64 }else{ 65 System.out.println("回答错误"); 66 wrongNum++; 67 } 68 }else if(fuhao3.equals("*")){ //*-* 69 System.out.println(num1 +"*"+num2 +"-"+num3 +"*"+num4+"="); 70 double sum = scan.nextDouble(); 71 if(sum == (num1*num2-num3*num4)){ 72 System.out.println("回答正确"); 73 rightNum++; 74 }else{ 75 System.out.println("回答错误"); 76 wrongNum++; 77 } 78 }else{ //*-/ 79 System.out.println(num1 +"*"+num2 +"-"+num3 +"/"+num4+"="); 80 double sum = scan.nextDouble(); 81 if(sum == (num1*num2-num3/num4)){ 82 System.out.println("回答正确"); 83 rightNum++; 84 }else{ 85 System.out.println("回答错误"); 86 wrongNum++; 87 } 88 89 } 90 91 }else if (fuhao2.equals("*")){ //**+ 92 if(fuhao3.equals("+")) { 93 System.out.println(num1 +"*"+num2 +"*"+num3 +"+"+num4+"="); 94 double sum = scan.nextDouble(); 95 if(sum == (num1*num2*num3+num4)){ 96 System.out.println("回答正确"); 97 rightNum++; 98 }else{ 99 System.out.println("回答错误"); 100 wrongNum++; 101 } 102 103 }else if(fuhao3.equals("-")){ //**- 104 System.out.println(num1 +"*"+num2 +"*"+num3 +"-"+num4+"="); 105 double sum = scan.nextDouble(); 106 if(sum == (num1*num2*num3-num4)){ 107 System.out.println("回答正确"); 108 rightNum++; 109 }else{ 110 System.out.println("回答错误"); 111 wrongNum++; 112 } 113 }else if(fuhao3.equals("*")){ //*** 114 System.out.println(num1 +"*"+num2 +"*"+num3 +"*"+num4+"="); 115 double sum = scan.nextDouble(); 116 if(sum == (num1*num2*num3*num4)){ 117 System.out.println("回答正确"); 118 rightNum++; 119 }else{ 120 System.out.println("回答错误"); 121 wrongNum++; 122 } 123 }else{ //**/ 124 System.out.println(num1 +"*"+num2 +"*"+num3 +"/"+num4+"="); 125 double sum = scan.nextDouble(); 126 if(sum == (num1*num2*num3/num4)){ 127 System.out.println("回答正确"); 128 rightNum++; 129 }else{ 130 System.out.println("回答错误"); 131 wrongNum++; 132 } 133 } 134 }else{ //*/+ 135 if(fuhao3.equals("+")) { 136 System.out.println(num1 +"*"+num2 +"/"+num3 +"+"+num4+"="); 137 double sum = scan.nextDouble(); 138 if(sum == (num1*num2/num3+num4)){ 139 System.out.println("回答正确"); 140 rightNum++; 141 }else{ 142 System.out.println("回答错误"); 143 wrongNum++; 144 } 145 146 }else if(fuhao3.equals("-")){ //*/- 147 System.out.println(num1 +"*"+num2 +"/"+num3 +"-"+num4+"="); 148 double sum = scan.nextDouble(); 149 if(sum == (num1*num2/num3-num4)){ 150 System.out.println("回答正确"); 151 rightNum++; 152 }else{ 153 System.out.println("回答错误"); 154 wrongNum++; 155 } 156 }else if(fuhao3.equals("*")){ //*/* 157 System.out.println(num1 +"*"+num2 +"/"+num3 +"*"+num4+"="); 158 double sum = scan.nextDouble(); 159 if(sum == (num1*num2/num3*num4)){ 160 System.out.println("回答正确"); 161 rightNum++; 162 }else{ 163 System.out.println("回答错误"); 164 wrongNum++; 165 } 166 }else{ //*// 167 System.out.println(num1 +"*"+num2 +"/"+num3 +"/"+num4+"="); 168 double sum = scan.nextDouble(); 169 if(sum == (num1*num2/num3/num4)){ 170 System.out.println("回答正确"); 171 rightNum++; 172 }else{ 173 System.out.println("回答错误"); 174 wrongNum++; 175 } 176 } 177 } 178
7、第一位符号位除号时
1 else{ //第一位除法开始 2 if(fuhao2.equals("+")){ 3 if(fuhao3.equals("+")) { //最后一位符号 4 System.out.println(num1 +"/"+num2 +"+"+num3 +"+"+num4+"=");// /++ 5 double sum = scan.nextDouble(); 6 if(sum == (num1/num2+num3+num4)){ 7 System.out.println("回答正确"); 8 rightNum++; 9 }else{ 10 System.out.println("回答错误"); 11 wrongNum++; 12 } 13 14 }else if(fuhao3.equals("-")){ ///+- 15 System.out.println(num1 +"/"+num2 +"+"+num3 +"-"+num4+"="); 16 double sum = scan.nextDouble(); 17 if(sum == (num1/num2+num3-num4)){ 18 System.out.println("回答正确"); 19 rightNum++; 20 }else{ 21 System.out.println("回答错误"); 22 wrongNum++; 23 } 24 }else if(fuhao3.equals("*")){ ///+* 25 System.out.println(num1 +"/"+num2 +"+"+num3 +"*"+num4+"="); 26 double sum = scan.nextDouble(); 27 if(sum == (num1/num2+num3*num4)){ 28 System.out.println("回答正确"); 29 rightNum++; 30 }else{ 31 System.out.println("回答错误"); 32 wrongNum++; 33 } 34 }else{ ///+/ 35 System.out.println(num1 +"/"+num2 +"+"+num3 +"/"+num4+"="); 36 double sum = scan.nextDouble(); 37 if(sum == (num1/num2+num3/num4)){ 38 System.out.println("回答正确"); 39 rightNum++; 40 }else{ 41 System.out.println("回答错误"); 42 wrongNum++; 43 } 44 45 } 46 }else if (fuhao2.equals("-")){ ///-+ 47 if(fuhao3.equals("+")) { 48 System.out.println(num1 +"/"+num2 +"-"+num3 +"+"+num4+"="); 49 double sum = scan.nextDouble(); 50 if(sum == (num1/num2-num3+num4)){ 51 System.out.println("回答正确"); 52 rightNum++; 53 }else{ 54 System.out.println("回答错误"); 55 wrongNum++; 56 } 57 58 }else if(fuhao3.equals("-")){ ///-- 59 System.out.println(num1 +"/"+num2 +"-"+num3 +"-"+num4+"="); 60 double sum = scan.nextDouble(); 61 if(sum == (num1/num2-num3-num4)){ 62 System.out.println("回答正确"); 63 rightNum++; 64 }else{ 65 System.out.println("回答错误"); 66 wrongNum++; 67 } 68 }else if(fuhao3.equals("*")){ ///-* 69 System.out.println(num1 +"/"+num2 +"-"+num3 +"*"+num4+"="); 70 double sum = scan.nextDouble(); 71 if(sum == (num1/num2-num3*num4)){ 72 System.out.println("回答正确"); 73 rightNum++; 74 }else{ 75 System.out.println("回答错误"); 76 wrongNum++; 77 } 78 }else{ ///-/ 79 System.out.println(num1 +"/"+num2 +"-"+num3 +"/"+num4+"="); 80 double sum = scan.nextDouble(); 81 if(sum == (num1/num2-num3/num4)){ 82 System.out.println("回答正确"); 83 rightNum++; 84 }else{ 85 System.out.println("回答错误"); 86 wrongNum++; 87 } 88 89 } 90 91 }else if (fuhao2.equals("*")){ ///*+ 92 if(fuhao3.equals("+")) { 93 System.out.println(num1 +"/"+num2 +"*"+num3 +"+"+num4+"="); 94 double sum = scan.nextDouble(); 95 if(sum == (num1/num2*num3+num4)){ 96 System.out.println("回答正确"); 97 rightNum++; 98 }else{ 99 System.out.println("回答错误"); 100 wrongNum++; 101 } 102 103 }else if(fuhao3.equals("-")){ ///*- 104 System.out.println(num1 +"/"+num2 +"*"+num3 +"-"+num4+"="); 105 double sum = scan.nextDouble(); 106 if(sum == (num1/num2*num3-num4)){ 107 System.out.println("回答正确"); 108 rightNum++; 109 }else{ 110 System.out.println("回答错误"); 111 wrongNum++; 112 } 113 }else if(fuhao3.equals("*")){ ///** 114 System.out.println(num1 +"/"+num2 +"*"+num3 +"*"+num4+"="); 115 double sum = scan.nextDouble(); 116 if(sum == (num1/num2*num3*num4)){ 117 System.out.println("回答正确"); 118 rightNum++; 119 }else{ 120 System.out.println("回答错误"); 121 wrongNum++; 122 } 123 }else{ ///*/ 124 System.out.println(num1 +"/"+num2 +"*"+num3 +"/"+num4+"="); 125 double sum = scan.nextDouble(); 126 if(sum == (num1/num2*num3/num4)){ 127 System.out.println("回答正确"); 128 rightNum++; 129 }else{ 130 System.out.println("回答错误"); 131 wrongNum++; 132 } 133 } 134 }else{ ////+ 135 if(fuhao3.equals("+")) { 136 System.out.println(num1 +"/"+num2 +"/"+num3 +"+"+num4+"="); 137 double sum = scan.nextDouble(); 138 if(sum == (num1/num2/num3+num4)){ 139 System.out.println("回答正确"); 140 rightNum++; 141 }else{ 142 System.out.println("回答错误"); 143 wrongNum++; 144 } 145 146 }else if(fuhao3.equals("-")){ ////- 147 System.out.println(num1 +"/"+num2 +"/"+num3 +"-"+num4+"="); 148 double sum = scan.nextDouble(); 149 if(sum == (num1/num2/num3-num4)){ 150 System.out.println("回答正确"); 151 rightNum++; 152 }else{ 153 System.out.println("回答错误"); 154 wrongNum++; 155 } 156 }else if(fuhao3.equals("*")){ ////* 157 System.out.println(num1 +"/"+num2 +"/"+num3 +"*"+num4+"="); 158 double sum = scan.nextDouble(); 159 if(sum == (num1/num2/num3*num4)){ 160 System.out.println("回答正确"); 161 rightNum++; 162 }else{ 163 System.out.println("回答错误"); 164 wrongNum++; 165 } 166 }else{ ///// 167 System.out.println(num1 +"/"+num2 +"/"+num3 +"/"+num4+"="); 168 double sum = scan.nextDouble(); 169 if(sum == (num1/num2/num3/num4)){ 170 System.out.println("回答正确"); 171 rightNum++; 172 }else{ 173 System.out.println("回答错误"); 174 wrongNum++; 175 } 176 } 177 } 178 } 179
8、最后对正确或者错误的题数进行统计
1 System.out.println("正确的题数是:"+ rightNum);
2 System.out.println("错误的题数是:"+ wrongNum);
运行结果
1 第1题 2 3/5/1/4= 3 0 4 回答正确 5 第2题 6 3/7/6/2= 7 0 8 回答正确 9 第3题 10 5-8-2-7= 11 3 12 回答错误 13 第4题 14 6-2-9-9= 15 -14 16 回答正确 17 第5题 18 2-7-4-6= 19 -15 20 回答正确 21 第6题 22 1-6-5-1= 23 -10 24 回答错误 25 第7题 26 1*9*1*7= 27 28 63 29 回答正确 30 第8题 31 7/7/5/4= 32 0 33 回答正确 34 第9题 35 7-2-5-1= 36 -1 37 回答正确 38 第10题 39 4*7*2*8= 40 889 41 回答错误 42 正确的题数是:7 43 错误的题数是:3
总结:实现4位数混合运算,我的办法虽然简单易懂但是特别的麻烦。还有很多值得改进的地方。
合作照片 地点寝室。
时间: 2024-10-24 19:57:40