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()获取开始和结束的时间,然后用两个时间相减来获取运行时间;
相关文章推荐
- eterna框架-介绍
- FastCgi与PHP-fpm关系
- 数据结构排序算法之简单插入排序
- SQL常用语句(补)
- R语言学习笔记1
- 在CentOS搭建Git服务器
- 面试题:两个链表相交的交点
- LeetCode Two Sum
- stl关联容器
- C++第三次实验—友元类
- SQL常用语句
- [HDU 4569] Special equations (数学+技巧+脑洞)
- Ciel and Dancing
- LeetCode *** 39. Combination Sum
- 推理逻辑算法--------------问题抽象与数学建模
- 深入剖析Java中的装箱和拆箱
- MySQL 基础 —— DQL(数据查询)
- nyoj 756 重建二叉树(总结)
- 四种聚类算法的比较
- MVC、MVP、MVVM的区别