1. 程式人生 > >java程序:學生練習加法運算

java程序:學生練習加法運算

currentTimeMillis number

import java.util.Scanner;
public class AdditionQuiz {
public static void main(String[] args){
int number1 = (int)(System.currentTimeMillis()%10);
int number2 = (int)(System.currentTimeMillis()*7%10);
System.out.println("What is "+number1+"+"+number2+"?");
Scanner input = new Scanner(System.in);
int answer = input.nextInt();
System.out.println(number1+"+"+number2+"="+answer+" is "+(number1+number2==answer));

}
}
產生隨機數的方法有很多,現在使用System.currentTimeMillis()方法產生隨機數。(number1+number2==answer)的結果是一個布爾值:true或false.

java程序:學生練習加法運算