How can I convert a double variable that gets increased by 1000 a second (or 1 every second but has three decimal points) into an int (or whatever type variable works but preferably an int) that will increase by one hundred every second and after it increased by one hundred (the second) I do what I want and reset it to 0? (My problem with my way of doing it, is that my double variable is not being equal to every of those 1000 numbers it's probably jumping over a lot, so I can't just do
.
Just in case here is a little of what you might ask for:
1 | if (oneThousand% 10.0 == 0 ) hundred++; |
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.*; import java.text.*; /** * This class should be added in to the world when needed, and will continue displaying the amount of seconds that elapsed since the addition to the world. * * @author Nosson1459 * @version 12/6/2016 */ public class Timer extends Actor { private long startTime; // this is the starting time of the scenario private long stopTime; // this is for figuring out the diffrence in time from beginning to now private double elapsedTime= 0.0000 ; // this is what timer is equal private double millis= 0.0000 ; private int hun= 00 ; private int second= 00 ; private int minute= 00 ; private int hour= 00 ; private static final Color transparent = new Color( 0 , 0 , 0 , 0 ); private int a= 0 ; private String prefix; private GreenfootImage time; public Timer(String prefix) { this .prefix=prefix; updateTime(); } /** * Act - do whatever the Timer wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (a== 0 ) { startTime=System.currentTimeMillis(); a= 1 ; // this "if" is so that startTime is initialized only after "Run" is pressed but still only initialized once } reset(); updateTime(); } /** * Update the image on screen to show seconds stopwatch */ private void updateTime() { Stopwatch world=(Stopwatch)getWorld(); if (a != 0 ) { if (world.stopped== true ) { startTime+=(world.startedTime-world.stoppedTime); world.stopped= false ; } if (world.click== true && world.middleStop== true && world.buttonStopped== false ) { startTime+=(world.startedMiddle-world.stoppedMiddle); world.click= false ; world.middleStop= false ; } if (world.buttonStopped== false ) { stopTime=System.currentTimeMillis(); //gets current time in milliseconds elapsedTime=(stopTime-startTime); //converts time to seconds } if (elapsedTime% 10.0 == 0 ) { hun++; } if (hun== 100 ) { second++; hun= 00 ; } if (second== 60 ) { minute++; second= 00 ; } if (minute== 60 ) { hour++; minute= 00 ; } } time= new GreenfootImage(prefix + new DecimalFormat( "00" ).format(hour) + ":" + new DecimalFormat( "00" ).format(minute) + ":" + new DecimalFormat( "00" ).format(second) + ":" + new DecimalFormat( "00" ).format(hun), 25 , Color.BLACK, transparent); // the above line makes the timer image, the decimal format makes the timer only have one decimal place (0.0) (you could do more if you do (0.00) etc. setImage(time); } private void reset() { Stopwatch world=(Stopwatch)getWorld(); if (world.reset== true ) { a= 0 ; elapsedTime= 0.0000 ; millis= 0.0000 ; hun= 00 ; second= 00 ; minute= 00 ; hour= 00 ; } } // new DecimalFormat("00").format(hun) } |