3.结对编程成果报告(小学生四则运算的出题程序,Java实现)
程序名称:小学生四则运算的出题程序
先附上代码:
package com.makequestion;
import java.text.DecimalFormat;
import java.util.Random;
import java.util.Scanner;
public class start {
private static int questionNum = 100;// 循环的次数
private static int rightResult = -1;// 正确答案
private static int inputResult = -1;// 输入的答案
private static int rightNum = 0;// 正确的个数
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 1; i <= questionNum; i++) {
judgeResult(rightResult, inputResult);
}
showResult();
}
public static void showResult() {
if (rightNum == questionNum) {
System.out.println("非常 不错,全对!我已经没什么可以教你的了。");
} else {
DecimalFormat df = new DecimalFormat("0.00%");// 格式化输出
String rightRateStr;// 存储比率的字符串
double rightRate;// 正确率
rightRate = (double) rightNum / (double) questionNum;
rightRateStr = df.format(rightRate);
System.out.println("正确率为:" + rightRateStr);
System.out.println("革命尚未成功,同志仍需努力!");
}
}
public static void judgeResult(int rightResult, int inputResult) {// 判断对错
rightResult = makeQuestion();
inputResult = inputResult();
if (rightResult == inputResult) {
rightNum += 1;
System.out.print("\t正确");
System.out.println();
} else {
System.out.print("\t错误,正确答案为:" + rightResult);
System.out.println();
}
}
public static int makeQuestion() {
int operation = -1;// 标记运算符
int num1 = -1;// 运算数1
int num2 = -1;// 运算数2
int rightResult = -1;// 正确的运算结果
Random random = new Random();
num1 = random.nextInt(100);// 随机生成在[0,100]范围内的运算数1
num2 = random.nextInt(100);// 随机生成在[0,100]范围内的运算数2
operation = random.nextInt(3);// 产生随机数,代表运算符0为+,1为-,2为*,3为/
switch (operation) {
case 0: {
rightResult = num1 + num2;// 计算结果
System.out.print(num1 + "+" + num2 + "=");// 输出题目
break;
}
case 1: {
rightResult = num1 - num2;
System.out.print(num1 + "-" + num2 + "=");
break;
}
case 2: {
rightResult = num1 * num2;
System.out.print(num1 + "*" + num2 + "=");
break;
}
case 3: {
if (num2 == 0) {
num2 = num2 + 1;
}
rightResult = num1 / num2;
System.out.print(num1 + "/" + num2 + "=");
break;
}
}
return rightResult;
}
public static int inputResult() {// 用户输入答案
int result = -1;
Scanner input = new Scanner(System.in);
result = input.nextInt();// 用户输入她的答案
// input.close();
return result;
}
}
输入输出:
为了测试方便questionNum取为3。
1.有错误的情况
2.全对的情况
照片涉及隐私,恕难附加。