您的位置:首页 > 其它

System.currentTimeMillis()

2016-04-15 21:45 288 查看
package java_experiment_9;
import java.util.Scanner;
public class Java_experiment_9 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String output = " ";
int T_answer = 0;
int F_answer = 0;
long time1 = System.currentTimeMillis();
for(int i=0;i<10;i++){
int number1 = (int) (Math.random()*10)+(int) (Math.random()*5);
int number2 = (int) (Math.random()*10)+(int) (Math.random()*5);
int result = number1+number2;
System.out.print(number1+"+"+number2+"=");
int answer = input.nextInt();
if(answer==result){
System.out.print(" 正确!\n");
T_answer++;
}
else{
System.out.print(" 错误!\n");
F_answer++;
}
output += "\n" + number1 + "+" + number2 + "=" + answer + ((answer==result)?" 正确!":" 错误!");

}
long time2 = System.currentTimeMillis();
long time = (time2-time1)*1000;
System.out.println(time);
System.out.println("答对的数量是:"+T_answer);
System.out.println("打错的数量是:"+F_answer);
System.out.println(output);
}

}


1.该源代码是一个产生10个15之内加法的小程序,最后有一个结果以及时间的统计;

2.可以通过声明一个字符串变量,然后对其赋值,还可以在循环中对其赋值,让其记录相关的东西;

3.可以用System.currentTimeMillis()来获取一个时间,可以通过在程序的头和尾调用System.currentTimeMillis()获取开始和结束的时间,然后用两个时间相减来获取运行时间;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: