随笔1026 四则运算作业

博客班级 https://edu.cnblogs.com/campus/ahgc/AHPU-SE-19
作业要求 https://edu.cnblogs.com/campus/ahgc/AHPU-SE-19/homework/11376
作业目标 写一个能自动生成小学四则运算题目的程序
学号 3190704241
`package daolunzuoye1026;

import java.util.Random;
import java.util.Scanner;

public class daolun {
public static void main(String[] args) {
int e = 0, r = 0;
System.out.println("欢迎来到四则运算测试软件!");
System.out.println("做题前请认真读题,祝您考试愉快!");
Random r1 = new Random();
for (int i = 0; i < 7; i++) {
int m, n;
int a, b, c;
m = r1.nextInt() % 10;
n = m % 4;

         switch (n) {
         case 0:
             a = new Random().nextInt((int) Math.pow(10, 2)) + 1;
             b = new Random().nextInt((int) Math.pow(10, 2)) + 1;
             System.out.println(a + "+" + "" + b + "=?");
             Scanner x1 = new Scanner(System.in);
             c = x1.nextInt();
             if (c != a + b) {
                 e++;
                 System.out.println("回答错误!!");
             } else {
                 r++;
                 System.out.println("回答正确!!");
             }

             break;

         case 1:
             a = new Random().nextInt((int) Math.pow(10, 2)) + 1;
             b = new Random().nextInt((int) Math.pow(10, 2)) + 1;
             System.out.println(a + "-" + " " + b + " =?");
             Scanner x2 = new Scanner(System.in);
             c = x2.nextInt();
             if (c != a - b) {
                 e++;
                 System.out.println("回答错误!!");
             } else {
                 r++;
                 System.out.println("回答正确!!");
             }

             break;

         case 2:
             a = new Random().nextInt((int) Math.pow(10, 2)) + 1;
             b = new Random().nextInt((int) Math.pow(10, 2)) + 1;
             System.out.println(a + "*" + " " + b + " " + "=?");
             Scanner y1 = new Scanner(System.in);
             c = y1.nextInt();
             if (c != a * b) {
                 e++;
                 System.out.println("回答错误!!");
             } else {
                 r++;
                 System.out.println("回答正确!!");
             }

             break;

         case 3:
             double d;
             a = new Random().nextInt((int) Math.pow(10, 2)) + 1;
             b = new Random().nextInt((int) Math.pow(10, 2)) + 1;
             if (b == 0)
                 b++;
             System.out.println(a + "/" + " " + b + " " + "=?");
             Scanner y2 = new Scanner(System.in);
             d = y2.nextDouble();
             if (d != (a / b) / 1.00) {
                 e++;
                 System.out.println("回答错误!!");
             } else {
                 r++;
                 System.out.println("回答正确!!");

             }

             break;

         }
     }
     System.out.println("考试结束!!");
     System.out.println("您一共做了:" + (r + e) + "道题目,其中正确的有" + r + "道,错误的有" + e + "道!");
 }

}`

C语言:
`#include<stdio.h>

include<math.h>

include<windows.h>

int right=0;
int wrong=0;
void add()
{
int a,b,c;
a=rand()%100;
b=rand()%100;
printf("请回答:\n\t\t %d + %d = ",a,b);
scanf("%d",&c);
if(a+bc)
{
printf("回答正确!\n");
right++;
}
else
{
printf("回答错误!\n");
wrong++;
}
}
void minu()
{
int a,b,c;
a=rand()%100;
b=rand()%100;
printf("请回答:\n\t\t %d - %d = ",a,b);
scanf("%d",&c);
if(a-b
c)
{
printf("回答正确!\n");
right++;
}
else
{
printf("回答错误!\n");
wrong++;
}
}
void mul()
{
int a,b,c;
a=rand()%100;
b=rand()%100;
printf("请回答:\n\t\t %d * %d = ",a,b);
scanf("%d",&c);
if(a*bc)
{
printf("回答正确!\n");
right++;
}
else
{
printf("回答错误!\n");
wrong++;
}
}
void di()
{
int a,b,c;
a=rand()%100;
b=rand()%100;
printf("请回答:\n\t\t %d / %d = ",a,b);
scanf("%d",&c);
if(a/b
c)
{
printf("回答正确!\n");
right++;
}
else
{
printf("回答错误!\n");
wrong++;
}
}
void main()
{
int choise;
int con=0;
printf("\n\t\t\t欢迎进入小学简易四则运算\n\n");
while(1)
{
printf("请选择:\n");
printf("\t\t\t 加法运算(请输入1)\n");
printf("\t\t\t 减法运算(请输入2)\n");
printf("\t\t\t 乘法运算(请输入3)\n");
printf("\t\t\t 除法运算(请输入4)\n");
printf("\t\t\t 退出运算(请输入5)\n");
if(con0)
scanf("%d",&choise);
switch(choise)
{
case 1:
add();
break;
case 2:
minu();
break;
case 3:
mul();
break;
case 4:
di();
break;
case 5:
return;
}
printf("\n\t\t\t继续运算?(请输入1)\n");
printf("\n\t\t\t重新选择?(请输入2)\n");
printf("\n\t\t\t退出运算?(请输入3)\n");
scanf("%d",&con);
if(con
1)
con=1;
else if(con2)
con=0;
else if(con
3)
break;
else
printf("抱歉!,你输入的指令有误!请重新输入!\n");
}
printf("您总共完成了 %d 道题\n正确 %d 道\n错误 %d 道\n",right+wrong,right,wrong);
}`

表格:

总结:很可惜,自己好多知识都忘记了,只实现了部分功能,还需要继续学习;整数部分加了进去,真分数其实定义一个函数放进去也就行了,认识到自己的不足。

posted @ 2020-10-27 00:03  I乐风  阅读(125)  评论(0)    收藏  举报