实验开始设计:
实验实际时间:
源代码
1 package zqn; 2 import java.util.*; 3 public class cal2 4 { 5 public static void main(String[] args) 6 { 7 // TODO Auto-generated method stub 8 Scanner sc =new Scanner(System.in);//创建一个输入对象,从系统输入 9 Random rand=new Random();//创建一个随机访问数对象 10 System.out.println("\t================================"); 11 System.out.print("\t\t请问你想计算几道题目:"); 12 int n=sc.nextInt();//输入一个整数n 13 System.out.println("\t================================"); 14 System.out.println(); 15 System.out.println("\t================================"); 16 System.out.println("\t\t接下来,请开动你的脑经开始答题吧!"); 17 System.out.println("\t================================"); 18 int right=0;//统计结果正确的个数 19 int error=0;//统计结果错误的个数 20 int result=0;//统计加,减,乘,除的结果 21 int c;//从键盘接受一个你输入的答案 22 for(int i=1;i<=n;i++) 23 { 24 int a=rand.nextInt(10);//产生0-10的随机数 25 int b=rand.nextInt(10); 26 int op=rand.nextInt(4); 27 switch (op) 28 { 29 case 0: 30 System.out.println("\t"+a+"+"+b+"=?"); 31 System.out.print("\t请你输入计算后的答案:"); 32 c=sc.nextInt(); 33 System.out.println(); 34 result=a+b; 35 if(c==result) right=right+1; 36 else error=error+1; 37 break; 38 case 1: 39 System.out.println("\t"+a+"-"+b+"=?"); 40 System.out.print("\t请你输入计算后的答案:"); 41 c=sc.nextInt(); 42 System.out.println(); 43 result=a-b; 44 if(c==result) right=right+1; 45 else error=error+1; 46 break; 47 case 2: 48 System.out.println("\t"+a+"*"+b+"=?"); 49 System.out.print("\t请你输入计算后的答案:"); 50 c=sc.nextInt(); 51 System.out.println(); 52 result=a*b; 53 if(c==result) right=right+1; 54 else error=error+1; 55 break; 56 case 3: 57 if(b!=0) 58 { 59 System.out.println("\t"+a+"/"+b+"=?"); 60 System.out.print("\t请你输入计算后的答案:"); 61 c=sc.nextInt(); 62 System.out.println(); 63 result=a/b; 64 if(c==result) right=right+1; 65 else error=error+1; 66 } 67 break; 68 default: 69 System.out.println("\t"+a+"+"+b+"=?"); 70 System.out.print("\t请你输入计算后的答案:"); 71 c=sc.nextInt(); 72 System.out.println(); 73 result=a+b; 74 if(c==result) right=right+1; 75 else error=error+1; 76 break; 77 } 78 } 79 System.out.println("\t================================"); 80 if (error<=0) System.out.println("\t\t恭喜你!!! 答对了"+right+"题!"); 81 else 82 { 83 System.out.println("\t\t恭喜你!!! 答对了"+right+"题!"); 84 System.out.println("\t\t很失望!!! 你答错了"+error+"题!"); 85 } 86 System.out.println("\t================================"); 87 sc.close();//关闭输入 88 } 89 }
测试共享
缺陷记录