100以内加减法混合出题 进位加法 && 退位减法

100以内加减法混合出题
进位加法 && 退位减法

'''

100以内加减法混合出题
进位加法 && 退位减法
Author: 
Date: 
'''

import random
exp1,exp2 = '',''
str1,str2 = '',''
start = 15
j = 1

# count = int(input('Input question number:\n'))
count = 100 #63
while j < count:
    print("-while----j------",j)
    if j < count:
        flag1 = random.choice(['+','-'])
        flag2 = random.choice(['+','-'])
        a = random.randint(start,100)
        b = random.randint(start,100)
        c = random.randint(start,100)
        print("-----flag1,flag2------",flag1,flag2)
        yua = a % 10
        yub = b % 10
        yuc = c % 10
        if flag1 == '+' and flag2 == '+' :
            result1 = a + b
            result2 = result1 + c
            yu1 = yua + yub
            yu2 = result1 % 10 + yuc
            if result2 >100 or yu1 < 10 or yu2 <10:
                print("-----exp1------",result1,result2)
                continue
        elif flag1 == '+' and flag2 == '-' :
            result1 = a + b
            result2 = result1 - c
            yu1 = yua + yub
            if result1 >100 or result2 < 0 or yu1 <= 10 or result1 % 10 >= c :
                print("-----exp2------",result1,result2)
                continue
        elif flag1 == '-' and flag2 == '+' :
            result1 = a - b
            result2 = result1 + c
            yu2 = result1 % 10 + yuc
            if  result2 >100 or result1 < 0 or yua>=yub or yu2<=10:
                print("-----exp3------",result1,result2)
                continue
        elif flag1 == '-' and flag2 == '-' :
            result1 = a - b
            result2 = result1 - c
            if result1  < 0 or result2 < 0 or yua>=yub or result1%10>=yuc :
                print("-----exp4------",result1,result2)
                continue
        print("-----j------",j)
        a = str(a).ljust(2," ")
        b = str(b).ljust(2," ")
        c = str(c).ljust(2," ")
        jj = str(j).ljust(2," ")
        exp1 = jj + '.' + a + " " + flag1 + " " + b + " "+ flag2 + " " + c + ' ='
        exp2 = jj + '.' + a + " " + flag1 + " " + b + " "+ flag2 + " " + c +' =' + str(result2)
        print("-----exp------",exp1)
        if j % 3 == 0:
            str1 = str1 + exp1 + '\n\n\n\n\n\n\n\n\n\n'#\n\n\n\n\n\n\n\n
            str2 = str2 + exp2 + '\n'
            #print("-----j------",j)
        else:
            str1 = str1 + exp1 + '\t\t'
            str2 = str2 + exp2 + '\t\t'
        j = j + 1
with open('数学混合加减运算.doc','w') as f:
    f.write(str1)
with open('数学混合加减运算答案.txt','w') as f:
    f.write(str2)
# with open('数学混合加减运算和答案.txt','w') as f:
#     f.write(str1)
#     f.write('\n\n\n\n\n\n\n\n\n')
#     f.write(str2)
print(count,'No Answer')
print(str1)
print(count,'With Answer:')
print(str2)

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很好,这是一个简单的数学题目生成程序,让我们一步一步来实现它。 首先,我们需要定义一些常量来表示题目类型,例如: ```java public static final int GRADE_ONE_ADD_SUB_1 = 1; // 一年级 10以内加减法:1步计算 public static final int GRADE_ONE_ADD_SUB_2 = 2; // 一年级 10以内加减法:2步计算 public static final int GRADE_ONE_ADD_SUB_3 = 3; // 一年级 10-20的加减法:1步计算和2步计算 public static final int GRADE_ONE_COMBINE = 4; // 一年级 综合训练 public static final int GRADE_TWO_ADD_NO_CARRY = 5; // 二年级 100以内进位加法 public static final int GRADE_TWO_SUB_NO_BORROW = 6; // 二年级 100以内退位减法 public static final int GRADE_TWO_ADD_CARRY = 7; // 二年级 100以内进位加法 public static final int GRADE_TWO_SUB_BORROW = 8; // 二年级 100以内退位减法 public static final int GRADE_TWO_COMBINE = 9; // 二年级 100以内连加连减 综合训练 ``` 接下来,我们需要定义一个函数来生成指定类型和数量的题目。我们可以使用一个 switch 语句来根据类型生成不同种类的题目,例如: ```java public static List<String> generateQuestions(int type, int count) { List<String> result = new ArrayList<>(); Random random = new Random(); switch (type) { case GRADE_ONE_ADD_SUB_1: // 一年级 10以内加减法:1步计算 for (int i = 0; i < count; i++) { int a = random.nextInt(10); int b = random.nextInt(10); if (random.nextBoolean()) { // 随机生成加法减法 result.add(a + " + " + b + " = "); } else { result.add(a + " - " + b + " = "); } } break; case GRADE_ONE_ADD_SUB_2: // 一年级 10以内加减法:2步计算 for (int i = 0; i < count; i++) { int a = random.nextInt(10); int b = random.nextInt(10); int c = random.nextInt(10); result.add(a + " - " + b + " - " + c + " = "); } break; case GRADE_ONE_ADD_SUB_3: // 一年级 10-20的加减法:1步计算和2步计算 for (int i = 0; i < count; i++) { int a = random.nextInt(11) + 10; // 10-20之间的随机数 int b = random.nextInt(11); int c = random.nextInt(11); int d = random.nextInt(11); if (random.nextBoolean()) { // 随机生成加法减法 result.add(a + " + " + b + " - " + c + " = "); } else { result.add(a + " - " + b + " + " + c + " - " + d + " = "); } } break; case GRADE_ONE_COMBINE: // 一年级 综合训练 for (int i = 0; i < count; i++) { int type2 = random.nextInt(3) + 1; // 随机选择上述三种类型的一种 result.addAll(generateQuestions(type2, 1)); } break; case GRADE_TWO_ADD_NO_CARRY: // 二年级 100以内进位加法 for (int i = 0; i < count; i++) { int a = random.nextInt(90) + 10; // 10-99之间的随机数 int b = random.nextInt(10); result.add(a + " + " + b + " = "); } break; case GRADE_TWO_SUB_NO_BORROW: // 二年级 100以内退位减法 for (int i = 0; i < count; i++) { int a = random.nextInt(90) + 10; // 10-99之间的随机数 int b = random.nextInt(a % 10 + 1); // 减数不大于个位数 result.add(a + " - " + b + " = "); } break; case GRADE_TWO_ADD_CARRY: // 二年级 100以内进位加法 for (int i = 0; i < count; i++) { int a = random.nextInt(90) + 10; // 10-99之间的随机数 int b = random.nextInt(10); if (a % 10 + b >= 10) { // 需要进位 result.add(a + " + " + b + " = "); } else { result.add(a + " + " + b + " = "); } } break; case GRADE_TWO_SUB_BORROW: // 二年级 100以内退位减法 for (int i = 0; i < count; i++) { int a = random.nextInt(90) + 10; // 10-99之间的随机数 int b = random.nextInt(10); if (a % 10 < b) { // 需要退位 result.add(a + " - " + b + " = "); } else { result.add(a + " - " + b + " = "); } } break; case GRADE_TWO_COMBINE: // 二年级 100以内连加连减 综合训练 for (int i = 0; i < count; i++) { int type2 = random.nextInt(4) + 5; // 随机选择上述四种类型的一种 result.addAll(generateQuestions(type2, 1)); } break; } return result; } ``` 接下来,我们需要实现一个函数来记录错题和错误次数。我们可以使用一个 Map 来记录每个题目的错误次数,例如: ```java public static Map<String, Integer> wrongQuestions = new HashMap<>(); public static void recordWrongQuestion(String question) { if (wrongQuestions.containsKey(question)) { wrongQuestions.put(question, wrongQuestions.get(question) + 1); } else { wrongQuestions.put(question, 1); } } ``` 最后,我们需要实现一个函数来读取和保存用户数据。我们可以使用 Java 的序列化机制来实现,例如: ```java public static void saveUserData(String username, UserData userData) throws IOException { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(username + ".dat")); oos.writeObject(userData); oos.close(); } public static UserData loadUserData(String username) throws IOException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream(new FileInputStream(username + ".dat")); UserData userData = (UserData) ois.readObject(); ois.close(); return userData; } ``` 完整代码如下:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值