1 import java.util.ArrayList; 2 import java.util.Random; 3 import java.util.Scanner; 4 import java.util.ArrayList; 5 import java.util.Random; 6 import java.util.Scanner; 7 8 public class CountdownThread extends Thread { 9 private static int seconds; 10 private static int count=0; 11 12 public CountdownThread(int seconds) { 13 this.seconds = seconds; 14 } 15 16 @Override 17 public void run() { 18 try { 19 while (seconds > 0) { 20 Thread.sleep(1000); 21 seconds--; 22 } 23 System.out.println("倒计时结束!"); 24 System.out.println("正确个数为:"+count+" 准确率为:"+count/(float)30); 25 System.exit(0); 26 } catch (InterruptedException e) { 27 e.printStackTrace(); 28 } 29 } 30 31 public static void main(String[] args) { 32 Scanner sc =new Scanner(System.in); 33 System.out.print("请输入时间:"); 34 seconds=sc.nextInt(); 35 CountdownThread thread = new CountdownThread(seconds); 36 thread.start(); 37 ArrayList<data> list = new ArrayList<>(); 38 Input(list); 39 print(list); 40 } 41 42 //添加数据 43 public static void Input(ArrayList<data> list){ 44 int flag=0; 45 while (flag<=30){ 46 Random r=new Random(); 47 int data1=r.nextInt(100); 48 int data2=r.nextInt(100); 49 int number=r.nextInt(100)%4; 50 data d=new data(data1,number,data2); 51 switch (number){ 52 case 0:{ 53 if(!check(list,data1,number,data2)){ 54 break; 55 } 56 list.add(d); 57 flag++; 58 break; 59 } 60 case 1:{ 61 if(!check(list,data1,number,data2)){ 62 break; 63 } 64 int result=data1-data2; 65 if(result>=0){ 66 list.add(d); 67 flag++; 68 } 69 break; 70 } 71 case 2:{ 72 if(!check(list,data1,number,data2)){ 73 break; 74 } 75 int result=data1*data2; 76 if(result<1000){ 77 list.add(d); 78 flag++; 79 } 80 break; 81 } 82 case 3:{ 83 if(!check(list,data1,number,data2)){ 84 break; 85 } 86 int result = data1 % data2; 87 if(result==0 && data1>data2){ 88 list.add(d); 89 flag++; 90 } 91 break; 92 } 93 } 94 } 95 } 96 //判断数据是否重复 97 public static boolean check(ArrayList<data> list,int data1,int number,int data2){ 98 for (int i = 0; i < list.size(); i++) { 99 data d=list.get(i); 100 int a=d.getData1(); 101 int b=d.getData2(); 102 int c=d.getData3(); 103 if((a==data1)&&(b==number)&&(c==data2)){ 104 return false; 105 } 106 } 107 return true; 108 } 109 //打印数据 110 public static void print(ArrayList<data> list){ 111 Scanner sc=new Scanner(System.in); 112 113 for (int i = 0; i < list.size(); i++) { 114 data d=list.get(i); 115 if(d.getData2()==0){ 116 int number,result; 117 System.out.print(d.getData1()+"+"+d.getData3()+"="); 118 number=sc.nextInt(); 119 result=d.getData1()+d.getData3(); 120 if(number==result){ 121 count++; 122 } 123 System.out.println("剩余时间:"+seconds); 124 } 125 if(d.getData2()==1){ 126 int number,result; 127 System.out.print(d.getData1()+"-"+d.getData3()+"="); 128 number=sc.nextInt(); 129 result=d.getData1()-d.getData3(); 130 if(number==result){ 131 count++; 132 } 133 System.out.println("剩余时间:"+seconds); 134 } 135 if(d.getData2()==2){ 136 int number,result; 137 System.out.print(d.getData1()+"*"+d.getData3()+"="); 138 number=sc.nextInt(); 139 result=d.getData1()*d.getData3(); 140 if(number==result){ 141 count++; 142 } 143 System.out.println("剩余时间:"+seconds); 144 } 145 if(d.getData2()==3){ 146 int number,result; 147 System.out.print(d.getData1()+"/"+d.getData3()+"="); 148 number=sc.nextInt(); 149 result=d.getData1()*d.getData3(); 150 if(number==result){ 151 count++; 152 } 153 System.out.println("剩余时间:"+seconds); 154 } 155 } 156 System.out.println("正确个数为:"+count+" 准确率为:"+count/(float)30); 157 System.exit(0); 158 } 159 }
标签:java,9.18,int,list,System,number,result,课堂,out From: https://www.cnblogs.com/Lyh3012648079/p/17712916.html