10.24每日总结

在课上练习的基础上,实现输出加减法混合的运算题目列表。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package JavaTest;
 
import java.util.*;
import javax.script.*;
 
public class test1 {
 
   static char[] fu = {'+', '-', '*', '/'};  // 符号
   static String[] ti = new String[100];  // 题
   static String[] daan = new String[100];  // 答案
   static int tishu = 10// 题数
   static int caozuoshu = 2// 操作数
   static int fanwei = 100// 范围
 
   public static void choose() {
       Scanner s = new Scanner(System.in);
 
       System.out.println("**********************");
       System.out.println("*  口算题生成与答题   *");
       System.out.println("**********************");
 
       System.out.println("请选择操作:");
       System.out.println("1. 参数设置");
       System.out.println("2. 开始出题并答题");
       System.out.println("0. 退出");
 
       int x = s.nextInt();
       while (x != 0) {
           switch (x) {
               case 1:
                   canshu();
                   break;
               case 2:
                   chuti(tishu, caozuoshu, fanwei);
                   suandaan();
                   zuoti();
                   break;
               default:
                   System.out.println("请输入 1-2 或 0 退出");
                   break;
           }
           System.out.println("**********************");
           System.out.println("*  口算题生成与答题   *");
           System.out.println("**********************");
 
           System.out.println("请选择操作:");
           System.out.println("1. 参数设置");
           System.out.println("2. 开始出题并答题");
           System.out.println("0. 退出");
 
           x = s.nextInt();
       }
   }
 
   // 参数设置
   public static void canshu() {
       Scanner c = new Scanner(System.in);
       System.out.println("**********************");
       System.out.println("*      参数设置      *");
       System.out.println("**********************");
 
       System.out.print("请输入题数:");
       tishu = c.nextInt();
 
       System.out.print("请输入操作数:");
       caozuoshu = c.nextInt();
 
       System.out.print("请输入范围:");
       fanwei = c.nextInt();
       System.out.println("参数设置完成");
   }
 
   // 出题
   public static void chuti(int tishu, int caozuoshu, int fanwei) {
       int a = 0;
       int c = 0;
       int d = 0;
       int e = 0;
       Random r = new Random();
       for (int i = 0; i < tishu; i++) {
           StringBuilder b = new StringBuilder();
           if (caozuoshu == 2) {
               d = r.nextInt(4);
               if (d == 3) {
                   while (true) {
                       e = r.nextInt(fanwei + 1);
                       c = r.nextInt(fanwei + 1);
                       a = c * e;
                       if (a <= fanwei && c != 0)
                           break;
                   }
               } else {
                   a = r.nextInt(fanwei + 1);
                   d = r.nextInt(3);
                   c = r.nextInt(fanwei + 1);
               }
               b.append(a);
               b.append(' ').append(fu[d]).append(' ');
               b.append(c);
           } else {
               for (int j = 1; j <= 2 * caozuoshu - 1; j += 2) {
                   a = r.nextInt(fanwei + 1);
                   b.append(a);
                   if (j != 2 * caozuoshu - 1) {
                       b.append(' ').append(fu[r.nextInt(3)]).append(' ');
                   }
               }
           }
 
           ti[i] = b.toString();
       }
   }
 
   // 查重
   public static boolean same() {
       for (int i = 0; i < tishu; i++) {
           for (int j = i + 1; j < tishu; j++) {
               if (ti[i].equals(ti[j])) {
                   return true;
               }
           }
       }
       return false;
   }
 
   // 算答案
   public static void suandaan() {
       ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
       ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("nashorn");
 
       for (int i = 0; i < tishu; i++) {
           try {
               String result = String.valueOf(scriptEngine.eval(ti[i]));
               daan[i] = result;
           } catch (ScriptException e) {
               e.printStackTrace();
           }
       }
   }
 
   // 做题
   public static void zuoti() {
       Scanner indaan = new Scanner(System.in);
       String an = "";
 
       System.out.println("**********************");
       System.out.println("*       开始答题       *");
       System.out.println("**********************");
 
       int count = 0;
       for (int i = 0; i < tishu; i++) {
           if (!same()) {
               count++;
               System.out.print(count + ".\t" + ti[i] + " = ");
               an = indaan.next();
               if (an.equals(daan[i])) {
                   System.out.println("√");
               } else {
                   System.out.println("×");
               }
           } else {
               i--;
           }
       }
       System.out.println("**********************");
       System.out.println("*       答题完成       *");
       System.out.println("**********************");
 
       System.out.println("正确率:" + (double) (tishu - count) * 100 / tishu + "%");
   }
 
   public static void main(String[] args) {
       choose();
   }
}

  

 

posted @   漏网鲨鱼  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示