Java学习八
今天对之前写的随机出题的程序进行了优化,我将程序分成了一个主方法和多个子方法,并且给程序添加了一个新功能:在线答题。这个功能在最后能够输出答题的正确率,并且将错题将集合的方式输出。
下面是我的源代码
1 import java.util.ArrayList; 2 import java.util.Random; 3 import java.util.Scanner; 4 5 public class Reerzhu { 6 static ArrayList<String> s=new ArrayList<String>(); 7 static int[] m=new int[100]; 8 static int[] n=new int[100]; 9 static int i; 10 static Random r=new Random(); 11 static Scanner sc=new Scanner(System.in); 12 static int num; 13 public static void main(String[] args) { 14 System.out.print("请输入数值范围:"); 15 num=sc.nextInt(); 16 System.out.println("请选择:1、加减乘除;2、加减;3、带括号;4、在线答题"); 17 int choice=sc.nextInt(); 18 select(choice); 19 } 20 21 //判断重复,输出运算式 22 public static void repeat() { 23 System.out.print("请输入题目数:"); 24 int number=sc.nextInt(); 25 for(i=0;i<number;i++) { 26 m[i]=r.nextInt(num)+1; 27 n[i]=r.nextInt(num)+1; 28 for(int j=0;j<i;j++) { 29 if(m[i]==m[j]&&n[i]==n[j]) { 30 m[i]=r.nextInt(num)+1; 31 } 32 } 33 int k=r.nextInt(4)+1; 34 switch(k) { 35 case 0:System.out.println(m[i]+"+"+n[i]+"=");break; 36 case 1:System.out.println(m[i]+"-"+n[i]+"=");break; 37 case 2:System.out.println(m[i]+"*"+n[i]+"=");break; 38 case 3:System.out.println(m[i]+"/"+n[i]+"=");break; 39 } 40 } 41 } 42 43 public static void jiajian() { 44 System.out.print("请输入题目数:"); 45 int number=sc.nextInt(); 46 for(i=0;i<number;i++) { 47 m[i]=r.nextInt(num)+1; 48 n[i]=r.nextInt(num)+1; 49 for(int j=0;j<i;j++) { 50 if(m[i]==m[j]&&n[i]==n[j]) { 51 m[i]=r.nextInt(num)+1; 52 } 53 } 54 int k=r.nextInt(4)+1; 55 switch(k) { 56 case 0:System.out.println(m[i]+"+"+n[i]+"=");break; 57 case 1:System.out.println(m[i]+"-"+n[i]+"=");break; 58 } 59 } 60 } 61 62 //带括号 63 public static void kuohao() { 64 int[] a=new int[100]; 65 int[] b=new int[100]; 66 System.out.print("请输入题目数:"); 67 int number=sc.nextInt(); 68 for(i=0;i<number;i++) { 69 m[i]=r.nextInt(num)+1; 70 n[i]=r.nextInt(num)+1; 71 a[i]=r.nextInt(num)+1; 72 b[i]=r.nextInt(num)+1; 73 for(int j=0;j<i;j++) { 74 if(m[i]==m[j]&&n[i]==n[j]) { 75 m[i]=r.nextInt(num)+1; 76 } 77 } 78 int k=r.nextInt(4)+1; 79 switch(k) { 80 case 0:System.out.println("("+m[i]+"+"+n[i]+")"+"+"+a[i]+"*"+b[i]+"=");break; 81 case 1:System.out.println("("+m[i]+"-"+n[i]+")"+"-"+b[i]+"/"+a[i]+"=");break; 82 case 2:System.out.println(m[i]+"+"+"("+n[i]+"+"+a[i]+")"+"*"+b[i]+"=");break; 83 case 3:System.out.println("("+m[i]+"-"+n[i]+")"+"/"+a[i]+"-"+b[i]+"=");break; 84 } 85 } 86 } 87 88 //选择 89 public static void select(int choice) { 90 switch(choice) { 91 case 1:repeat();break; 92 case 2:jiajian();break; 93 case 3:kuohao();break; 94 case 4:answer();break; 95 } 96 } 97 98 //在线答题 99 public static void answer() { 100 double count=0; 101 System.out.print("请输入题目数:"); 102 int number=sc.nextInt(); 103 for(i=0;i<number;i++) { 104 m[i]=r.nextInt(num)+1; 105 n[i]=r.nextInt(num)+1; 106 for(int j=0;j<i;j++) { 107 if(m[i]==m[j]&&n[i]==n[j]) { 108 m[i]=r.nextInt(num)+1; 109 } 110 } 111 int k=r.nextInt(4)+1; 112 switch(k) { 113 case 0:System.out.println(m[i]+"+"+n[i]+"="); 114 double a=sc.nextInt(); 115 System.out.println("正确答案:"+(m[i]+n[i])); 116 if(a!=(m[i]+n[i])) { 117 String e=m[i]+"+"+n[i]+"="; 118 s.add(e); 119 count++; 120 } 121 break; 122 case 1:System.out.println(m[i]+"-"+n[i]+"="); 123 double b=sc.nextInt(); 124 System.out.println("正确答案:"+(m[i]-n[i])); 125 if(b!=(m[i]-n[i])) { 126 String e=m[i]+"-"+n[i]+"="; 127 s.add(e); 128 count++; 129 } 130 break; 131 case 2:System.out.println(m[i]+"*"+n[i]+"="); 132 double c=sc.nextInt(); 133 System.out.println("正确答案:"+(m[i]*n[i])); 134 if(c!=(m[i]*n[i])) { 135 String e=m[i]+"*"+n[i]+"="; 136 s.add(e); 137 count++; 138 } 139 break; 140 case 3:System.out.println(m[i]+"/"+n[i]+"="); 141 double d=sc.nextInt(); 142 System.out.println("正确答案:"+(m[i]/n[i])); 143 if(d!=(m[i]/n[i])) { 144 String e=m[i]+"/"+n[i]+"="; 145 s.add(e); 146 count++; 147 } 148 break; 149 } 150 } 151 System.out.println("正确率:"+(100-(count/number)*100)+"%"); 152 System.out.println("错题数目:"+count); 153 System.out.println("错题:"+s); 154 } 155 156 }
由于对程序进行优化,所以我只测试了新增的功能,其他的测试我之前的博客中有,下面是测试截图