1. 程式人生 > >java程序:產生隨機數

java程序:產生隨機數

math random number answer temp

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之間的隨機數。

java程序:產生隨機數