标签:
一、背景
阿超有个儿子上小学二年级,老师每天让家长给孩子出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写的:
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
import java.io.*;
public class Pratices {
public static void main(String[] args) {
new Pratices().list_Pratices();
}
public int random_Num(int range) {
return (int) (Math.random() * range);
}
public void list_Pratices() {
int right = 0;
int wrongtimes = 0;
int num_1, num_2, temp;
int type = random_Num(4);
int score = 0;
int count = 1;
System.out.println("请输入题目数量:");
Scanner sc = new Scanner(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 < num2))
{
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+ " = ");//除法
else
System.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("回答错误 ");
else
System.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, int my_Answer,
int count) {
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(int count) {
int my_Answer = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(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;
}
}
return my_Answer;
}
public int getScore(int wrongtimes) {
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";
}
}
标签:
原文地址:http://www.cnblogs.com/chengxs/p/5263935.html