Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class stopwatch {
- public long thetime = 0;
- public long stoppedtime = 0;
- public boolean ticking = false;
- public static void main(String[] args) {
- stopwatch s = new stopwatch();
- Scanner sc = new Scanner(System.in);
- boolean loop = true;
- while (loop = true) {
- System.out.println("1 start 2 is started 3 stop 4 "
- + "reset 5 check time 6 stop");
- int i = sc.nextInt();
- if (i == 1) {
- s.start();
- } else if (i == 2) {
- System.out.println(s.isStarted());
- } else if (i == 3) {
- s.stop();
- } else if (i == 4) {
- s.reset();
- } else if (i == 5) {
- System.out.println ("saved time is "+ s.time()+" Seconds");
- } else if (i == 6) {
- System.out.println("closing");
- loop = false;
- break;
- } else {
- System.out.println("invalid");
- }
- }
- }
- public void start() {
- if (ticking == true) {
- thetime = thetime;
- } else {
- thetime = System.currentTimeMillis();
- ticking = true;
- }
- }
- public boolean isStarted() {
- return ticking;
- }
- public void stop() {
- if (ticking == false) {
- stoppedtime = stoppedtime;
- } else {
- stoppedtime = thetime;
- ticking = false;
- }
- }
- public void reset() {
- thetime = 0;
- stoppedtime = 0;
- }
- public double time() {
- double seconds = 1000.000000;
- double currenttime = 0;
- double saved = stoppedtime;
- if (ticking == true) {
- currenttime = ((System.currentTimeMillis() - thetime)/seconds);
- return currenttime;
- }else {
- currenttime = (stoppedtime/seconds);
- return currenttime;
- }
- }}enter code here
Add Comment
Please, Sign In to add comment