c语言小学生四则运算出题_软件工程第一次作业,小学生四则运算的出题程序...

本文介绍了如何利用Java编程实现一个为小学生生成加减乘除练习题的程序,确保结果不出现负数,并支持真分数运算。程序包括题目生成、答案判断和得分系统,旨在帮助家长轻松完成孩子的家庭作业。
摘要由CSDN通过智能技术生成

一、背景

阿超有个儿子上小学二年级,老师每天让家长给孩子出30道加减法题,虽然不多,但是每天都做也算是个负担,阿超作为一个老牌程序员当然想用计算机来解决这个小问题,目前对于这个问题对于任何语言都不是问题,比如:

C/C++、C#、Java、Python、VB、JavaScript、Perl……

具体要求如下:

能自动生成小学四则运算题目(注意是给小学生用的,要是结果出现负数的话他们会迷茫的!)

除了整数外,还要支持真分数的四则运算

请大家用任何一种自己擅长的语言来编写这段程序,并把程序的介绍和自己编写的过程写一个博客

二、分析

(一) 自己擅长的是c语言,准备用c语言,但是自己学的java,想用java试试,支持真分数运算,如果用C语言,我们可以这么考虑,a,b,c,d随机生成。

a/b      c/d,

(1)可能存在a,b,c,d;a和b存在公约数,c和d存在公约数。

解 决办法:先求a和b的最大公约数m,先求c和d的最大公约数n,然后a=a/m;b=b/m;c=c/m;d=d/m;,然后就可以算加"+"法 (a*d+b*c)/(b*d);减“-”法(a*d-b*c)/(b*d);乘“*”法a*c/(b*d);除法“/”,判断一下分母是否为0,分数符 号直接输出;

(2)可能存在a>b,c>d的情况,真假分数情况。输出直接输出符号“/”。

(二)有判断正确和错误,每答一次就判断一次,回答正确和回答错误,一次性答对是10分,答两次才答对得5分,答三次才答对得3分。

(三)输入一个数,知道出题的数目,随机产生的题数目,多输也会提示输入错误。

三、代码部分

我用的是java写的:

importjava.util.InputMismatchException;

importjava.util.Random;

importjava.util.Scanner;

import java.io.*;

public classPratices {

public static voidmain(String[] args) {

newPratices().list_Pratices();

}

public int random_Num(intrange) {

return (int) (Math.random() *range);

}

public voidlist_Pratices() {

int right = 0;

int wrongtimes = 0;

intnum_1, num_2, temp;

int type = random_Num(4);

int score = 0;

int count = 1;

System.out.println("请输入题目数量:");

Scanner sc = newScanner(System.in);

int n =sc.nextInt();

while (count <=n) {

type = random_Num(2);

num1 = random_Num(100); //100以内随机数

num2 = random_Num(100); //100以内的随机数

wrongtimes = 0;

if (type == 0)

{

System.out.print("(" + count + ") " + num1 + " + " + num2+ " = ");//加法

}

else if(type == 1)

{

if ((num1

{

temp =num1;

num1 =num2;

num2 =temp;

}

System.out.print("(" + count + ") " + num1 + " - " + num2+ " = ");//减法

}

else if(type == 2)

System.out.print("(" + count + ") " + num1 + " * " + num2+ " = ");//乘法

}

else if(type == 3)

{

if(num2!=0)

System.out.print("(" + count + ") " + num1 + " / " + num2+ " = ");//除法

elseSystem.out.println("分母为零");

}

int answer = this.getAnswer(count);

boolean flag =check(num1, num2, type, answer, count);

if(flag) {

right++;

System.out.println("回答正确");

score += this.getScore(wrongtimes);

} else{

while (wrongtimes < 2) {

wrongtimes++;

System.out.println("回答错误 " + wrongtimes + " 次");

answer = this.getAnswer(count);

flag =check(num1, num2, type, answer, count);

if(flag) {

score += this.getScore(wrongtimes);

right++;

wrongtimes = 0;

break;

}

}

if (wrongtimes == 3)

System.out.println("回答错误 ");

elseSystem.out.println("回答正确");

}

count++;

}

System.out.println("回答正确 : " +right);

System.out.println("回答错误: " + (10 -right));

System.out.println("获得分数: " +score);

System.out.println(getDegree(score));

}

public boolean check(int num_1, int num_2, int type, intmy_Answer,

intcount) {

int answer = 0;

if (type == 1) {

answer = num_1 -num_2;

} else if (type == 0) {

answer = num_1 +num_2;

}

return my_Answer ==answer;

}

public int getAnswer(intcount) {

int my_Answer = 0;

BufferedReader br = new BufferedReader(newInputStreamReader(System.in));

try{

my_Answer =Integer.parseInt(br.readLine());

} catch(IOException e) {

e.printStackTrace();

} catch(NumberFormatException e) {

System.out.println("输入有误");

return 0;

} finally{

if (count >= n && (br != null)) {//不会超出输入的n

try{

br.close();

} catch(IOException e) {

e.printStackTrace();

}

br = null;

}

}

returnmy_Answer;

}

public int getScore(intwrongtimes) {

if (wrongtimes == 0) {

return 10;

} else if (wrongtimes == 1) {

return 7;

} else if (wrongtimes == 2) {

return 5;

} else

return 0;

}

public String getDegree(int score) {

if (score > 90)

return "SMART";

else if (score > 80)

return "GOOD";

else if (score > 70)

return "OK";

else if (score > 60)

return "PASS";

else

return "TRY AGAIN";

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值