计算10或100以内的加、减、乘运算
这是我闲来无事对Java语言程序设计(基础版第10版)的5.2.3‘多个减法测试题’进行的拓展题目
import java.util.Scanner;
public class QuizLoop {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int count;
int size=0;//size是指随机产生数字的大小
long startTime=System.currentTimeMillis();
System.out.print("请输入测试的题目总数:");
int textCount=input.nextInt();//题目数量
int correctCount=0;
for(count=1;count <= textCount;count++) {
System.out.println("\n请输入要做多少以内的算术:");
System.out.println("1.10以内(不包含10)\n2.100以内(不包含100)");
int i = input.nextInt();
// if(i==1) size=10;
//else if(i==2) size=100;
switch (i) {
case 1:
size= 10;
break;
case 2:
size = 100;
break;
}
int number1 = (int) (Math.random() * size);
int number2 = (int) (Math.random() * size);
System.out.println("选择算术类型‘+’、‘-’、‘*’");//除法是否加入还未决定
String c;
c = input.next();
System.out.println("What is "+ number1 + c + number2 +"=?");
int answer=input.nextInt();
switch(c) {
case "+": {
if (number1 + number2 == answer) {
System.out.println("You are correct!");
correctCount++;
} else
System.out.println("Your answer is wrong.\n" + number1 + "+" + number2 + " should be " + (number1 + number2));
break;
}
case "-": {
if (number1 - number2 == answer) {
System.out.println("You are correct!");
correctCount++;
} else
System.out.println("Your answer is wrong.\n" + number1 + "-" + number2 + " should be " + (number1 - number2));
break;
}
case "*": {
if (number1 * number2 == answer) {
System.out.println("You are correct!");
correctCount++;
} else
System.out.println("Your answer is wrong.\n" + number1 + "*" + number2 + " should be " + (number1 * number2));
break;
}
}
}
long endTime=System.currentTimeMillis();
long textTime=endTime-startTime;
System.out.println("Correct count is "+correctCount);
System.out.println("Text time is "+textTime/1000+" seconds");
}
}
运行截图: