第一次作业

import java.util.*;

public class calculate {
public static void main(String[] args) {
T t=new T();
}
}

class T {
Scanner sc = new Scanner(System.in);
Random Ra = new Random();
char[] cal = { '+', '-', '*', '/' };
char c;
int n = 10;
long startTime;
long endTime;
long time;

T() {
System.out.print("1:人选 2:机选");
int choice = sc.nextInt();
switch (choice) {
case 1: {
int right = 0;
System.out.print("请输入习题数:");
int user_cho = sc.nextInt();
startTime = System.currentTimeMillis();
for (int i = 0; i < user_cho; i++) {
int index = Ra.nextInt(cal.length);// 生成符号,随机数
c = cal[index];
int a = Ra.nextInt(100);
int b = Ra.nextInt(100);
if (c == '/' && b == 0) {
index = Ra.nextInt(cal.length);// 生成符号,随机数
c = cal[index];
}
System.out.printf("%d%c%d ", a, c, b);
int b1=a+c+b;
int a1 = sc.nextInt();
if(a1==b1){System.out.print("答案正确! ");}
else System.out.printf("答案有误!正确答案是%d ",b1);
}
endTime = System.currentTimeMillis();
time = endTime - startTime;
int time1 = (int) (time / 1000);
System.out.printf("一共用了%ds", time1);
}
break;
case 2: {
startTime = System.currentTimeMillis();
for (int i = 0; i < n; i++) {
int index = Ra.nextInt(cal.length);// 生成符号,随机数
c = cal[index];
int a = Ra.nextInt(100);
int b = Ra.nextInt(100);

System.out.printf("%d%c%d ", a, c, b);
int a2 = sc.nextInt();
if(a2==a+c+b){System.out.print("答案正确! ");}
else System.out.printf("答案有误!正确答案是%d ",a+c+b);
}
endTime = System.currentTimeMillis();
time = endTime - startTime;
int time1 = (int) (time / 1000);
System.out.printf("一共用了%ds", time1);
}
break;
}
}
}

原文地址:https://www.cnblogs.com/chenruting/p/4408945.html