java - Create a timed application -


i create simple game requires guess number within amount of turns (eg. 10). however, makes quite easy beat. need how keep track of how long game has been going on for.

this have thought of far (minus game logic), doesn't seem working

random rannum = new random();    double input;   // input long starttime; // time game started long curtime;   // time game ended      double randnum = rannum.nextint(100);  while (curtime > 1000){             curtime = system.currenttimemillis();             input = textio.getlndouble();              if (input = math.abs(randnum)){                 system.out.println("you got correct answer");              }   // end if statement              else {                 system.out.println("you did not have correct answer");                 system.out.println("the number was" + randnum + ".");              }   // end else statement          } // end while statement 

you have current timestamp before loop begins:

starttime = system.currenttimemillis(); //get timestamp  //the condition waaaay off, equals while (true) - more on later while (curtime > 1000){         input = textio.getlndouble();          if (input = math.abs(randnum)){             system.out.println("you got correct answer");          }   // end if statement          else {             system.out.println("you did not have correct answer");             system.out.println("the number was" + randnum + ".");          }   // end else statement      } // end while statement  curtime = system.currenttimemillis(); //get timestamp again  system.out.println("game took " + ((curtime-starttime)/1000) + " seconds"); 

from documentation of system class:

public static long currenttimemillis()

returns current time in milliseconds. note while unit of time of return value millisecond, granularity of value depends on underlying operating system , may larger. example, many operating systems measure time in units of tens of milliseconds.

see description of class date discussion of slight discrepancies may arise between "computer time" , coordinated universal time (utc).

returns:

the difference, measured in milliseconds, between current time , midnight, january 1, 1970 utc.

so duration of game, have take 2 timestamps, , usbtract latter former...

also, while loops condition way off... this

while (curtime > 1000){ 

is true... false on 1st of january, 1970, 00:00:00 00:00:01

you have this:

while(curtime-starttime > 10000) { //remember, value ms!    //...loop content    curtime = system.currenttimeinmillis(); //update timestamp } 

but not make game end after 10 seconds. if you'd restrict how long game can go on, different kind of cookie - you'd have have thread able accomplish that...


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -