关于控制语句的算术运算考试系统模拟
简单的考试系统的代码,需要进一步改进,和大家分享一下:
package day04;
import java.util.Scanner;
public class ArithmeticSoftSysFor {
?/**
? * 算术运算生成器,考试系统 1.产生随机数:两个数和一个符号 2.出题 3.输入答案 4.判断 5.求出几道题是对的,并算出分数
? */
?/**
? * 随机数方法 return int type 1或2
? */
?public static int getRander(int type) {
??int rand = 0;
??if (type == 1) {
???rand = (int) (Math.random() * 100);
??} else if (type == 2) {
???rand = (int) (Math.random() * 4);
??}
??return rand;
?}
?/**
? * 把op操作符:变为+/*-符号
? */
?public static char operator(int op) {
??char charop = '0';
??switch (op) {
??case 1:
???charop = '+';
???break;
??case 2:
???charop = '-';
???break;
??case 3:
???charop = '*';
???break;
??case 4:
???charop = '/';
???break;
??default:
???charop = '0';
??}
??return charop;
?}
?/**
? * 计算结果方法:参数char charop,int rand1,int rand2 retun int ans:答案
? *
? */
?public static int getResultAnswer(char charop, int rand1, int rand2) {
??int ans = -1;
??switch (charop) {
??case '+':
???ans = rand1 + rand2;
???break;
??case '-':
???ans = rand1 - rand2;
???break;
??case '*':
???ans = rand1 * rand2;
???break;
??case '/':
???ans = rand1 / rand2;
???break;
??default:
??}
??return ans;
?}
?/**
? * 从键盘输入一个数:有chance次机会
? */
?public static int getInput(int num, int chance) {
??Scanner sc = new Scanner(System.in);
??System.out.println("请输入一个数据:谢谢!你有" + chance + "机会!");
??num = sc.nextInt();
??
??return num;
?}
?/**
? * 从键盘输入一个数:
? */
?public static String getInputExam(String isExam) {
??Scanner sc = new Scanner(System.in);
??System.out.println("请输入一个数据yes or no:谢谢!");
??isExam = sc.nextLine();
??return isExam;
?}
?/**
? * 判断:输入的数据和答案是否相等 return boolean 参数:ans,num,sum,score
? */
?public static boolean getEqual(int ans, int num, int sum, int score,
???int count) {
??boolean flag = false;
??if (num == ans) {
???++count;// 计算题对的数目加1
???sum = sum + score;// 计算分数的和
???flag = true;
???System.out.println("恭喜你,回答正确!你答对了" + count + "道题,得到分数为:" + sum);
??} else if (num > ans) {
???flag = false;
???System.out.println("你输入太大力了!");
??} else {
???flag = false;
???System.out.println("你输入太大小了!");
??}
??return flag;
?}
?
?public static void main(String[] args) {
??// 随机数
??int rand1 = 0;
??int rand2 = 0;
??int op;
??int ans = 0;// 计算结果
??int num = 0;// 输入答案
??int score = 10;
??int sum = 0;
??int count = 0;
??int chance = 2;
??boolean isRight = false;
??char charop = '0';
??String isExam = "";
??start: while (true) {
???rand1 = getRander(1);
???rand2 = getRander(1);
???op = getRander(2);
???charop = operator(op);
???ans = getResultAnswer(charop, rand1, rand2);
???while (chance != 0) {
????num = getInput(num, chance);
????chance--;//减少一次机会
????isRight = getEqual(ans, num, sum, score, count);
????if (isRight) {
?????System.out.println("恭喜你,回答正确!你答对了" + count + "道题,得到分数为:"
???????+ sum);
?????// 是否还答题
?????isExam = getInputExam(isExam);
?????if (isExam.equals("yes")) {
??????continue start;
?????} else {
??????break start;
?????}
????} else {
?????System.out.println("对不起,回答错误!你答对了" + count + "道题,得到分数为:"
???????+ sum);
?????// 是否还答题
?????isExam = getInputExam(isExam);
?????if (isExam.equals("yes")) {
??????continue start;
?????} else {
??????break start;
?????}
????}
???}
??}
?}
}