public static void main(String[] args) { System.out.println("请输入题目数量:"); Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int k=0, count=0; while( k!=num) { System.out.println("第"+(k+1)+"题:"); Random ran = new Random(); int i=ran.nextInt(4); switch (i) { case 0: count+=add(); break; case 1: count+=sub(); break; case 2: count+=mul(); break; case 3: count+=div(); break; default: break; } k++; } System.out.println("计算结束,你答对了"+count+"题,获得"+count+"分。"); }
public static int add(){//加法 Random ran = new Random(); int a =ran.nextInt(101); int b=ran.nextInt(101-a); int an=a+b; System.out.println(a+"+"+b+"="+"?"); System.out.print("请输入答案:"); Scanner sc2 = new Scanner(System.in); int an2= sc2.nextInt(); if(an2==an) { return 1; } return 0; }
public static int sub(){//减法 Random ran = new Random(); int a =ran.nextInt(101); int b=ran.nextInt(a+1); int an=a-b; System.out.println(a+"-"+b+"="+"?"); System.out.print("请输入答案:"); Scanner sc2 = new Scanner(System.in); int an2= sc2.nextInt(); if(an2==an) { return 1; } return 0; }
public static int mul(){//乘法 Random ran = new Random(); int a =ran.nextInt(101); int b=ran.nextInt(100/a+1); int an=a*b; System.out.println(a+"*"+b+"="+"?"); System.out.print("请输入答案:"); Scanner sc2 = new Scanner(System.in); int an2= sc2.nextInt(); if(an2==an) { return 1; } return 0; }
public static int div(){//除法 Random ran = new Random(); int a =ran.nextInt(100)+1; int b=ran.nextInt(100/a)+1; int c=a*b; int an=c/a; System.out.println(c+"/"+a+"="+"?"); System.out.print("请输入答案:"); Scanner sc2 = new Scanner(System.in); int an2= sc2.nextInt(); if(an2==an) { return 1; } return 0; } }
@Test public void test() { System.out.println("请输入题目数量:"); Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int k=0, count=0; while( k!=num) { System.out.println("第"+(k+1)+"题:"); Random ran = new Random(); int i=ran.nextInt(4); switch (i) { case 0: count+=add(); break; case 1: count+=sub(); break; case 2: count+=mul(); break; case 3: count+=div(); break; default: break; } k++; } System.out.println("计算结束,你答对了"+count+"题,获得"+count+"分。"); }
public static int add(){//加法 Random ran = new Random(); int a =ran.nextInt(101); int b=ran.nextInt(101-a); int an=a+b; System.out.println(a+"+"+b+"="+"?"); System.out.print("请输入答案:"); Scanner sc2 = new Scanner(System.in); int an2= sc2.nextInt(); if(an2==an) { return 1; } return 0; }
public static int sub(){//减法 Random ran = new Random(); int a =ran.nextInt(101); int b=ran.nextInt(a+1); int an=a-b; System.out.println(a+"-"+b+"="+"?"); System.out.print("请输入答案:"); Scanner sc2 = new Scanner(System.in); int an2= sc2.nextInt(); if(an2==an) { return 1; } return 0; }
public static int mul(){//乘法 Random ran = new Random(); int a =ran.nextInt(101); int b=ran.nextInt(100/a+1); int an=a*b; System.out.println(a+"*"+b+"="+"?"); System.out.print("请输入答案:"); Scanner sc2 = new Scanner(System.in); int an2= sc2.nextInt(); if(an2==an) { return 1; } return 0; }
public static int div(){//除法 Random ran = new Random(); int a =ran.nextInt(100)+1; int b=ran.nextInt(100/a)+1; int c=a*b; int an=c/a; System.out.println(c+"/"+a+"="+"?"); System.out.print("请输入答案:"); Scanner sc2 = new Scanner(System.in); int an2= sc2.nextInt(); if(an2==an) { return 1; } return 0; } }