import java.util.Scanner; public class MathGame { public static void main(String[] args){ int number1=(int)( Math.random()*10); int number2=(int)(Math.random()*10); if(number1<number2){ int temp=number1; number1=number2; number2=temp; } System.out.println("what is "+number1+"-"+number2+" ?"); Scanner input = new Scanner(System.in); int answer = input.nextInt();
if(answer==number1-number2){
System.out.println("your answer is good!");
}
else{
System.out.println("your answer is false!");
System.out.println("the true answer is "+(number1-number2));
}
}
} 使用Math.random()方法来获得一个0.0到1.0之间的随机数。