9.18
全天满课,晚上在csdn搜索java如何获取时间的方法,完成课上测试的修改
package project3;
import java.util.Random;
import java.util.Scanner;
import java.util.Calendar;
public class demo {
public static void main(String[] args) throws InterruptedException {
calculate();
}
public static void calculate(){
Random ra = new Random();
int time=settingt();
int opent=opentime();
Scanner sc = new Scanner(System.in);
int[] i1 = generateRandomNumbers();
int[] i2 = generateRandomNumbers();
int n = 0;
int i =0;
int t = 0, f = 0;
do {
Random r = new Random();
int a = r.nextInt(4);
if(a==0) {
System.out.print(++n+"."+i1[i]+"+"+i2[i]+"=");
int result = sc.nextInt();
if(result==(i1[i]+i2[i])) {
t++;
}
if(nowtime()-opent>=time){
System.out.println("剩余0秒");
}else {
System.out.println("剩余" + (time-(nowtime() - opent)) + "秒");
}
}
if(a==1)
{
if(judgefu(i1[i],i2[i])) {
System.out.print(++n+"."+i1[i]+"-"+i2[i]+"=");
int result = sc.nextInt();
if(result==(i1[i]-i2[i])) {
t++;
}
if(nowtime()-opent>=time){
System.out.println("剩余0秒");
}else {
System.out.println("剩余" + (time-(nowtime() - opent)) + "秒");
}
}
}
if(a==2) {
if(judgefour(i1[i],i2[i])) {
System.out.print(++n+"."+i1[i]+"*"+i2[i]+"=");
int result = sc.nextInt();
if(result==(i1[i]*i2[i])) {
t++;
}
if(nowtime()-opent>=time){
System.out.println("剩余0秒");
}else {
System.out.println("剩余" + (time-(nowtime() - opent)) + "秒");
}
}
}
if(a==3){
if(judgechu(i1[i],i2[i])) {
System.out.print(++n+"."+i1[i]+"/"+i2[i]+"=");
int result = sc.nextInt();
if(result==(i1[i]/i2[i])) {
t++;
}
if(nowtime()-opent>=time){
System.out.println("剩余0秒");
}else {
System.out.println("剩余" + (time-(nowtime() - opent)) + "秒");
}
}
}
i++;
if(nowtime()-opent>=time){
System.out.println("时间到,停止答题。");
break;
}
}while(n!=30);
f=30-t;
System.out.println("此次答题正确数量为:"+t);
String v1 = String.format("%.2f", (double)t/30*100);
System.out.println("此次答题正确率为:"+v1+"%");
String v2 = String.format("%.2f", (double)f/30*100);
System.out.println("此次答题错误数量为:"+f);
System.out.println("此次答题错误率为:"+v2+"%");
}
public static int settingt(){
Scanner sc=new Scanner(System.in);
System.out.println("请输入做题时间(秒):");
int time=sc.nextInt();
return time;
}
public static int opentime(){
Calendar cal=Calendar.getInstance();
int hour=cal.get(Calendar.HOUR_OF_DAY);//时
int minute=cal.get(Calendar.MINUTE); //分
int second=cal.get(Calendar.SECOND); //秒
return hour*3600+minute*60+second;
}
public static int nowtime(){
Calendar cal=Calendar.getInstance();
int hour=cal.get(Calendar.HOUR_OF_DAY);//时
int minute=cal.get(Calendar.MINUTE); //分
int second=cal.get(Calendar.SECOND); //秒
return hour*3600+minute*60+second;
}
public static int[] generateRandomNumbers() {
int[] arr = new int[75];
Random random = new Random();
for (int i = 0; i < 75; i++) {
int num;
do {
num = random.nextInt(99)+1;
} while (contains(arr, num));
arr[i] = num;
}
return arr;
}
public static boolean contains(int[] arr, int num) {
for (int i : arr) {
if (i == num) {
return true;
}
}
return false;
}
public static boolean judgefu(int x,int y) {
if(x-y<0){
return false;
}else {
return true;
}
}
public static boolean judgefour(int x,int y) {
if(x*y>=1000) {
return false;
}else {
return true;
}
}
public static boolean judgechu(int x,int y) {
if(x%y==0) {
return true;
}else {
return false;
}
}
}