java写的,感觉方法很蠢。希望以后可以来改善一下。

package homework01;

import java.io.IOException;
import java.util.Scanner;

public class OperationBasic {
public static void main (String arg[]) throws IOException {
int answer;
int random1=(int)(1+Math.random()*(99-1+1));
int random2=(int)(1+Math.random()*(99-1+1));
int random=(int)(1+Math.random()*(4-1+1));
int random3=(int)(1+Math.random()*(10-1+1));
int random4=(int)(1+Math.random()*(10-1+1));
int random5=(int)(1+Math.random()*(99-1+1));
int random6=(int)(1+Math.random()*(99-1+1));
while(random5%random6!=0) {
random5=(int)(1+Math.random()*(99-1+1));
random6=(int)(1+Math.random()*(99-1+1));
}
System.out.println("The questions are as follows");
switch(random) {
case 1: System.out.println(random1+"+"+random2+"=?"); answer=random1 + random2; break;
case 2: System.out.println(random1+"-"+random2+"=?"); answer=random1 - random2; break;
case 3: System.out.println(random3+"*"+random4+"=?"); answer=random1 * random2; break;
default: System.out.println(random5+"/"+random6+"=?" ); answer=random1 / random2; break;
}
System.out.println("please input your anwer");
@SuppressWarnings("resource")
Scanner in =new Scanner(System.in);
int ans = in.nextInt();
if(ans==answer) {
System.out.println("congratulations!");
}else {
System.out.println("sorry- -");
}


}
}