Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (769)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (855)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  stopwatch not working  (Read 1897 times)
0 Members and 1 Guest are viewing this topic.
Offline westloar

Junior Devvie





« Posted 2011-04-03 22:40:44 »

I know there's good alternatives such as the GAGE timer, but I've just taught myself Java(ish) and wrote this program to get my head around some of the basics, can someone tell me why this program goes so catastrophically wrong when I run it?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
 public static void main(String[] args) {
        Runnable ticker = new Timer();
        Thread t = new Thread(ticker);
        t.start();
    }

}

class Timer implements Runnable {
    long oneClock;
    long twoClock;
    int gameLength = 25;
    long diff;
    long beforeTime;
    long afterTime;
    long realTime;
    boolean loop = false;

   public void run () {
        beforeTime = System.nanoTime();
        while (gameLength > 0){
            oneClock = System.nanoTime();
            loop = true;
           
            while (loop = true) {

                try {
                Thread.sleep(10); } catch (InterruptedException ex) {ex.printStackTrace(); }
                twoClock = System.nanoTime();

                diff = twoClock - oneClock;
                if (diff >= 1) {
                gameLength -= diff;
                System.out.println (this.gameLength) ;
                loop = false;
                } //second while close
            }
       } // 1st while close

        afterTime = System.nanoTime();
        realTime = afterTime - beforeTime;
        System.out.println("The end of the clock");
        System.out.println("The clock took" + realTime + "seconds to finish");
    }
}


That's the code, this is the output, which can tell is wrong:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
-9577724
-29163946
-58738110
-98324597
-147897743
-207477652
-277060474
-356637654
-446223307
-545811873
-655405063
-774993039
-904580078
-1044161048
-1193738515
-1353326594
-1522912453


I admit before I get flamed, I am a noob, but I'd appreciate some direction. Cheers guys.

Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2011-04-03 22:49:00 »

This code assigns 'true' to 'loop'...
1  
while (loop = true) {


You should replace it by:
1  
while (loop == true) {


Or even better:
1  
while (loop) {

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline zlandorf

Junior Devvie


Medals: 1
Projects: 1



« Reply #2 - Posted 2011-04-03 22:56:19 »

1  
 System.out.println("The clock took" + realTime + "seconds to finish");


Watch out, you're working with nanoseconds there, not seconds.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline westloar

Junior Devvie





« Reply #3 - Posted 2011-04-03 23:28:37 »

ha lol, can't believe I missed that thing with loop!!

I know I was working with nanoseconds, and in a previous version I multiplied the results by 1000000000 but I thought that might have been a root with the problem for some reason and edited that out.

ok so I changed the loop and multiplied each nanoTime by 1000000000 and now it gives me this result:
1  
2  
3  
4  
5  
6  
run:
767364121
-977696743
The end of the clock
The clock took19850560000000000seconds to finish
BUILD SUCCESSFUL (total time: 0 seconds)

Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2011-04-03 23:31:46 »

ok so I changed the loop and multiplied each nanoTime by 1000000000 and now it gives me this result:

So now you're using atto-seconds.

What about dividing by 1000000000?

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline westloar

Junior Devvie





« Reply #5 - Posted 2011-04-04 12:27:42 »

I discovered whythe stop watch was going crazy, I should have been dividing the nanoTime no multilying it

When I changed this, it worked, apart from it takes 24secs to finish not 25 :/ but that's a minor setback!

Cheers for the help Smiley
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1574 views)
2018-06-10 19:43:48

EgonOlsen (1658 views)
2018-06-10 19:43:44

EgonOlsen (1145 views)
2018-06-10 19:43:20

DesertCoockie (1573 views)
2018-05-13 18:23:11

nelsongames (1174 views)
2018-04-24 18:15:36

nelsongames (1665 views)
2018-04-24 18:14:32

ivj94 (2421 views)
2018-03-24 14:47:39

ivj94 (1631 views)
2018-03-24 14:46:31

ivj94 (2719 views)
2018-03-24 14:43:53

Solater (884 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!