学习内容:课堂测试二阶段三
代码示例:
import java.util.Random;
import java.util.Scanner;
public class Test3to2 {
public static void answer() {//在线答题
int num1;
int num2;
double sum;
long times = 0;
int data = 0;
int number1[]=new int[100];
int number2[]=new int[100];
int number3[]=new int[100];
int number4[]=new int[100];
int number5[]=new int[100];
int number6[]=new int[100];
int number7[]=new int[100];
int number8[]=new int[100];
int i,j,choice,rightnum=0,n1=0,n2=0,n3=0,n4=0;
char c;
double accurary;//正确率
char[] operator=new char[]{'+','-','*','/'};
Random rand = new Random();
Scanner in=new Scanner(System.in);
long startTime = System.currentTimeMillis(); //答题开始时间
System.out.println("请输入答题时间:");
int time=in.nextInt();
System.out.println("答题时间为"+time+"秒,答题开始......");
for(i=1;i<=30;i++) {
j=rand.nextInt(4);
c=operator[j];
num1=rand.nextInt(100);
num2=rand.nextInt(100);
Scanner sc=new Scanner(System.in);
if(c=='+') {
int result;
sum=num1+num2;
System.out.print("第"+i+"道题为:");
System.out.println(num1+"+"+num2+"=");
System.out.println("请输入计算结果:");
result=sc.nextInt();
System.out.println("答题结果为:"+num1+"+"+num2+"="+result);
if(result==sum) {
rightnum+=1;
}
else {
n1+=1;
number1[n1]=num1;
number2[n1]=num2;
}
long sysDate = System.currentTimeMillis();
times=sysDate-startTime;
if((double)times/1000>=time) {
System.out.println("答题时间到");
data=1;
break;
}
}
else if(c=='-') {
int result;
System.out.print("第"+i+"道题为:");
System.out.println(num1+"-"+num2+"=");
sum=num1-num2;
System.out.println("请输入计算结果:");
result=sc.nextInt();
System.out.println("答题结果为:"+num1+"-"+num2+"="+result);
if(result==sum) {
rightnum+=1;
}
else {
n2+=1;
number3[n2]=num1;
number4[n2]=num2;
}
long sysDate = System.currentTimeMillis();
times=sysDate-startTime;
if((double)times/1000>=time) {
System.out.println("答题时间到");
data=1;
break;
}
}
else if(c=='*') {
int result;
System.out.print("第"+i+"道题为:");
System.out.println(num1+"*"+num2+"=");
sum=num1*num2;
System.out.println("请输入计算结果:");
result=sc.nextInt();
System.out.println("答题结果为:"+num1+"*"+num2+"="+result);
if(result==sum) {
rightnum+=1;
}
else {
n3+=1;
number5[n3]=num1;
number6[n3]=num2;
}
long sysDate = System.currentTimeMillis();
times=sysDate-startTime;
if((double)times/1000>=time) {
System.out.println("答题时间到");
data=1;
break;
}
}
else if(c=='/') {
int result;
System.out.print("第"+i+"道题为:");
System.out.println(num1+"/"+num2+"=");
sum=(double)num1/(double)num2;
System.out.println("请输入计算结果:");
result=sc.nextInt();
System.out.println("答题结果为:"+num1+"/"+num2+"="+result);
if(result==sum) {
rightnum+=1;
}
else {
n4+=1;
number7[n4]=num1;
number8[n4]=num2;
}
long sysDate = System.currentTimeMillis();
times=sysDate-startTime;
if((double)times/1000>=time) {
System.out.println("答题时间到");
data=1;
break;
}
}
if(data==1) {
break;
}
}
accurary=(double)rightnum/30.0;
System.out.println("您的正确率为:"+accurary*100+"%");
System.out.println("您的错题集为:");
for(i=1;i<=n1;i++) {
System.out.println(number1[i]+"+"+number2[i]+"=");
}
for(i=1;i<=n2;i++) {
System.out.println(number3[i]+"-"+number4[i]+"=");
}
for(i=1;i<=n3;i++) {
System.out.println(number5[i]+"*"+number6[i]+"=");
}
for(i=1;i<=n4;i++) {
System.out.println(number7[i]+"/"+number8[i]+"=");
}
do {
System.out.println("错题重练请按1,结束练习请按0:");
Scanner sc=new Scanner(System.in);
choice=sc.nextInt();
if(choice==1) {
for(i=1;i<=n1;i++) {
double result;
int wrongnum=1;
System.out.println(number1[i]+"+"+number2[i]+"=");
sum=number1[i]+number2[i];
System.out.println("请输入计算结果:");
result=sc.nextDouble();
System.out.println("答题结果为:"+number1[i]+"+"+number2[i]+"="+result);
if(result!=sum) {
wrongnum+=1;
System.out.println("回答错误,该题错误次数为"+wrongnum);
}
else {
System.out.println("回答正确,本题错误次数为:"+wrongnum);
}
}
for(i=1;i<=n2;i++) {
double result;
int wrongnum=1;
System.out.println(number3[i]+"-"+number4[i]+"=");
sum=number1[i]-number2[i];
System.out.println("请输入计算结果:");
result=sc.nextDouble();
System.out.println("答题结果为:"+number3[i]+"-"+number4[i]+"="+result);
if(result!=sum) {
wrongnum+=1;
System.out.println("回答错误,该题错误次数为"+wrongnum);
}
else {
System.out.println("回答正确,本题错误次数为:"+wrongnum);
}
}
for(i=1;i<=n3;i++) {
double result;
int wrongnum=1;
System.out.println(number5[i]+"*"+number6[i]+"=");
sum=number1[i]*number2[i];
System.out.println("请输入计算结果:");
result=sc.nextDouble();
System.out.println("答题结果为:"+number5[i]+"*"+number6[i]+"="+result);
if(result!=sum) {
wrongnum+=1;
System.out.println("回答错误,该题错误次数为"+wrongnum);
}
else {
System.out.println("回答正确,本题错误次数为:"+wrongnum);
}
}
for(i=1;i<=n4;i++) {
double result;
int wrongnum=1;
System.out.println(number7[i]+"/"+number8[i]+"=");
sum=(double)number1[i]/(double)number2[i];
System.out.println("请输入计算结果:");
result=sc.nextDouble();
System.out.println("答题结果为:"+number7[i]+"/"+number8[i]+"="+result);
if(result!=sum) {
wrongnum+=1;
System.out.println("回答错误,该题错误次数为"+wrongnum);
}
else {
System.out.println("回答正确,本题错误次数为:"+wrongnum);
}
}
}
}while(choice==1);
}
public static void main(String[] args) {
answer();
}
}