鱼C论坛

 找回密码
 立即注册
查看: 3485|回复: 1

[已解决]一道简单运算题,求大神解答!

[复制链接]
发表于 2020-11-11 17:54:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
用java写一小学数学运算:要求编写一个模拟出题给小学生进行10以内加减法的程序。根据输入题数出题,判断做题是否正确,并在小学生完成所有题目后计算分数。
最佳答案
2020-11-11 22:51:22
出题类:
  1. package test;

  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.util.LinkedHashMap;
  6. import java.util.Map;
  7. import java.util.Random;
  8. import java.util.regex.Pattern;

  9. public class AddsubCalc {
  10.         private int num = -1; // 题数
  11.         private LinkedHashMap<String, int[]> subject; // key:题目,value[]:答案 结果
  12.         private static final char ADD = '+';
  13.         private static final char SUB = '-';

  14.         public int getNum() {
  15.                 return num;
  16.         }

  17.         /**
  18.          * 出题数目
  19.          * @throws IOException
  20.          */
  21.         public void setNum() throws IOException {
  22.                 System.out.print("请输入出题数目:");
  23.                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  24.                 String line = null;
  25.                 while(!isNumber(line = br.readLine())) {
  26.                         System.out.println("请输入数字!");
  27.                         System.out.print("请输入出题数目:");
  28.                         continue;
  29.                 }       
  30.                 this.num = Integer.parseInt(line);
  31.         }

  32.         public AddsubCalc() {
  33.                 super();
  34.                 this.subject = new LinkedHashMap<String, int[]>();
  35.         }

  36.         // 获取一个10以内的随机数
  37.         private int getWithinten() {
  38.                 return (int) (Math.random() * 10 + 1);
  39.         }

  40.         // 获取随机+ -运算符
  41.         private char getOperator() {
  42.                 return new Random().nextBoolean() ? ADD : SUB;
  43.         }

  44.         // 生成题目
  45.         private void makeSubject() {
  46.                 // 根据输入的数量生成表达式和结果
  47.                 int a, b, result;

  48.                 while (this.subject.size() < this.num) {
  49.                         StringBuilder s = new StringBuilder();
  50.                         a = getWithinten();
  51.                         b = getWithinten();
  52.                         s.append(a);
  53.                        
  54.                         if (getOperator() == ADD) {
  55.                                 result = a + b;
  56.                                 s.append(ADD);

  57.                         } else {
  58.                                 result = a - b;
  59.                                 s.append(SUB);
  60.                         }
  61.                         s.append(b);
  62.                         s.append(" = ");
  63.                         int tmp[] = { result, 0 };
  64.                         this.subject.put(s.toString(), tmp);
  65.                 }

  66.         }

  67.         /**
  68.          * 获取题目
  69.          */
  70.         public void outSubject() {
  71.                 if(this.num==-1) {
  72.                         throw new RuntimeException("请先调用setNum设置出题数目!");
  73.                 }
  74.                
  75.                 this.makeSubject();
  76.                 for (String subject : subject.keySet()) {
  77.                         System.out.println(subject);
  78.                 }
  79.         }

  80.         // 判断是否为Int
  81.         private boolean isInteger(String str) {
  82.                 Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
  83.                 return pattern.matcher(str.trim()).matches();
  84.         }

  85.         // 判断一个字符串是否由数字组成
  86.         public boolean isNumber(String temp) {
  87.                 char[] data = temp.trim().toCharArray();
  88.                 for (int x = 0; x < data.length; x++) {
  89.                         if (data[x] > '9' || data[x] < '0') {
  90.                                 return false;// 后续不再判断
  91.                         }
  92.                 }
  93.                 return true;// 如果全部验证通过返回true
  94.         }

  95.         /**
  96.          * 答题
  97.          * @throws IOException
  98.          */
  99.         public void getAnswer() throws IOException {
  100.                 if(this.subject.size()==0) {
  101.                         throw new RuntimeException("请先调用outSubject获取题目!");
  102.                 }
  103.                
  104.                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  105.                 String line = null;
  106.                 for (Map.Entry<String, int[]> m : this.subject.entrySet()) {
  107.                         String q = m.getKey();
  108.                         System.out.print(q);
  109.                         while (true) {

  110.                                 if (!isInteger(line = br.readLine())) {
  111.                                         System.out.println("请输入数字!");
  112.                                         System.out.print(q);
  113.                                         continue;
  114.                                 }
  115.                                 int[] v = m.getValue();
  116.                                 v[1] = Integer.parseInt(line);
  117.                                 m.setValue(v);
  118.                                 break;
  119.                         }
  120.                 }
  121.         }

  122.         /**
  123.          * 答题结果
  124.          */
  125.         public void getResult() {
  126.                 int totalScore = 0;
  127.                 for (Map.Entry<String, int[]> m : this.subject.entrySet()) {
  128.                         String k = m.getKey();
  129.                         int[] v = m.getValue();
  130.                         String tmp;
  131.                         tmp = k + Integer.toString(v[0]) + " 你 " + Integer.toString(v[1]);
  132.                         if (v[0] == v[1]) {
  133.                                 tmp = tmp + " √";
  134.                                 totalScore += 1;
  135.                         } else {
  136.                                 tmp = tmp + " ×";
  137.                         }
  138.                         System.out.println(tmp);
  139.                 }
  140.                 System.out.println("总分 " + this.subject.size() + "分 你的分数是:" + totalScore);
  141.         }

  142. }
复制代码


调用:
  1. package test;

  2. import java.io.IOException;

  3. public class MathProblem {

  4.         public static void main(String[] args) throws IOException {
  5.                 AddsubCalc calc = new AddsubCalc();               

  6.                 // 出题数目
  7.                 calc.setNum();
  8.                
  9.                 // 获取题目
  10.                 calc.outSubject();
  11.        
  12.                 // 答题
  13.                 System.out.println("请回答:");
  14.                 calc.getAnswer();
  15.                
  16.                 // 结果
  17.                 System.out.println("结果:");
  18.                 calc.getResult();

  19.         }

  20. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-11 22:51:22 | 显示全部楼层    本楼为最佳答案   
出题类:
  1. package test;

  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.util.LinkedHashMap;
  6. import java.util.Map;
  7. import java.util.Random;
  8. import java.util.regex.Pattern;

  9. public class AddsubCalc {
  10.         private int num = -1; // 题数
  11.         private LinkedHashMap<String, int[]> subject; // key:题目,value[]:答案 结果
  12.         private static final char ADD = '+';
  13.         private static final char SUB = '-';

  14.         public int getNum() {
  15.                 return num;
  16.         }

  17.         /**
  18.          * 出题数目
  19.          * @throws IOException
  20.          */
  21.         public void setNum() throws IOException {
  22.                 System.out.print("请输入出题数目:");
  23.                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  24.                 String line = null;
  25.                 while(!isNumber(line = br.readLine())) {
  26.                         System.out.println("请输入数字!");
  27.                         System.out.print("请输入出题数目:");
  28.                         continue;
  29.                 }       
  30.                 this.num = Integer.parseInt(line);
  31.         }

  32.         public AddsubCalc() {
  33.                 super();
  34.                 this.subject = new LinkedHashMap<String, int[]>();
  35.         }

  36.         // 获取一个10以内的随机数
  37.         private int getWithinten() {
  38.                 return (int) (Math.random() * 10 + 1);
  39.         }

  40.         // 获取随机+ -运算符
  41.         private char getOperator() {
  42.                 return new Random().nextBoolean() ? ADD : SUB;
  43.         }

  44.         // 生成题目
  45.         private void makeSubject() {
  46.                 // 根据输入的数量生成表达式和结果
  47.                 int a, b, result;

  48.                 while (this.subject.size() < this.num) {
  49.                         StringBuilder s = new StringBuilder();
  50.                         a = getWithinten();
  51.                         b = getWithinten();
  52.                         s.append(a);
  53.                        
  54.                         if (getOperator() == ADD) {
  55.                                 result = a + b;
  56.                                 s.append(ADD);

  57.                         } else {
  58.                                 result = a - b;
  59.                                 s.append(SUB);
  60.                         }
  61.                         s.append(b);
  62.                         s.append(" = ");
  63.                         int tmp[] = { result, 0 };
  64.                         this.subject.put(s.toString(), tmp);
  65.                 }

  66.         }

  67.         /**
  68.          * 获取题目
  69.          */
  70.         public void outSubject() {
  71.                 if(this.num==-1) {
  72.                         throw new RuntimeException("请先调用setNum设置出题数目!");
  73.                 }
  74.                
  75.                 this.makeSubject();
  76.                 for (String subject : subject.keySet()) {
  77.                         System.out.println(subject);
  78.                 }
  79.         }

  80.         // 判断是否为Int
  81.         private boolean isInteger(String str) {
  82.                 Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
  83.                 return pattern.matcher(str.trim()).matches();
  84.         }

  85.         // 判断一个字符串是否由数字组成
  86.         public boolean isNumber(String temp) {
  87.                 char[] data = temp.trim().toCharArray();
  88.                 for (int x = 0; x < data.length; x++) {
  89.                         if (data[x] > '9' || data[x] < '0') {
  90.                                 return false;// 后续不再判断
  91.                         }
  92.                 }
  93.                 return true;// 如果全部验证通过返回true
  94.         }

  95.         /**
  96.          * 答题
  97.          * @throws IOException
  98.          */
  99.         public void getAnswer() throws IOException {
  100.                 if(this.subject.size()==0) {
  101.                         throw new RuntimeException("请先调用outSubject获取题目!");
  102.                 }
  103.                
  104.                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  105.                 String line = null;
  106.                 for (Map.Entry<String, int[]> m : this.subject.entrySet()) {
  107.                         String q = m.getKey();
  108.                         System.out.print(q);
  109.                         while (true) {

  110.                                 if (!isInteger(line = br.readLine())) {
  111.                                         System.out.println("请输入数字!");
  112.                                         System.out.print(q);
  113.                                         continue;
  114.                                 }
  115.                                 int[] v = m.getValue();
  116.                                 v[1] = Integer.parseInt(line);
  117.                                 m.setValue(v);
  118.                                 break;
  119.                         }
  120.                 }
  121.         }

  122.         /**
  123.          * 答题结果
  124.          */
  125.         public void getResult() {
  126.                 int totalScore = 0;
  127.                 for (Map.Entry<String, int[]> m : this.subject.entrySet()) {
  128.                         String k = m.getKey();
  129.                         int[] v = m.getValue();
  130.                         String tmp;
  131.                         tmp = k + Integer.toString(v[0]) + " 你 " + Integer.toString(v[1]);
  132.                         if (v[0] == v[1]) {
  133.                                 tmp = tmp + " √";
  134.                                 totalScore += 1;
  135.                         } else {
  136.                                 tmp = tmp + " ×";
  137.                         }
  138.                         System.out.println(tmp);
  139.                 }
  140.                 System.out.println("总分 " + this.subject.size() + "分 你的分数是:" + totalScore);
  141.         }

  142. }
复制代码


调用:
  1. package test;

  2. import java.io.IOException;

  3. public class MathProblem {

  4.         public static void main(String[] args) throws IOException {
  5.                 AddsubCalc calc = new AddsubCalc();               

  6.                 // 出题数目
  7.                 calc.setNum();
  8.                
  9.                 // 获取题目
  10.                 calc.outSubject();
  11.        
  12.                 // 答题
  13.                 System.out.println("请回答:");
  14.                 calc.getAnswer();
  15.                
  16.                 // 结果
  17.                 System.out.println("结果:");
  18.                 calc.getResult();

  19.         }

  20. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-15 17:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表