package test1; import java.util.*; public class Si { /** * @param args */ public static void main(String[] args) { int a=0,b=0; Scanner sc = new Scanner(System.in); System.out.println("請輸入你的題量;"); int c=sc.nextInt(); Feng feng = new Feng(); feng.calc(c, a, b); } } package test1; import java.util.Scanner; public class Feng { public void calc(int c, int a, int b) { Scanner sc = new Scanner(System.in); for(int i=0;i<c;i++) { int s=1+(int)(Math.random()*4); switch(s) { case 1: int k; a=1+(int)(Math.random()*100); b=1+(int)(Math.random()*100); System.out.println(a+"+"+ b + "="); k=sc.nextInt(); if(k==a+b) System.out.println("答案正確;"); else System.out.println("答案錯誤,正確答案為"+(a+b)); break; case 2: int l=0; a=1+(int)(Math.random()*100); b=1+(int)(Math.random()*100); System.out.println(a+"-"+ b + "="); l=sc.nextInt(); if(l==a-b) { System.out.println("答案正確;"); if(a<b) System.out.println("改答案為負數;"); } else System.out.println("答案錯誤,正確答案為"+(a-b)); break; case 3: int m; a=1+(int)(Math.random()*10); b=1+(int)(Math.random()*10); System.out.println(a+"*"+ b + "="); m=sc.nextInt(); if(m==a*b) System.out.println("答案正確;"); else System.out.println("答案錯誤,正確答案為"+(a*b)); break; case 4: int n; a=1+(int)(Math.random()*10); b=1+(int)(Math.random()*10); System.out.println(a+"/"+ b + "="); n=sc.nextInt(); if(n==a/b) System.out.println("答案正確;"); else System.out.println("答案錯誤,正確答案為"+(a/b)); break; } } } }
模仿助教的方法,不知道對不對!!!!!