I am trying to write a program for a timer, in which every time the timer progresses 1/5, the time it takes for the output to update is longer. I have done his using a sleep thread and 5 if statements, in which for every time the timerSeconds variable goes down 1/5, it slows down the output. However, my if statements don't seem to be working and I am getting the same delay every time. Can anyone see what I am doing wrong? Thanks!
Notes: I have rewritten my if statements several times, and I wrote my delay based on a model of 5 intervals, in which the total delay should add up to 5 seconds.
I have also included a delay function which outputs the delay of the sleep thread, which is used purely to test if the system is accurate with its timing.
Thanks so much for any help!
The issue:
As you can see if you run my code, the delay is .4 seconds every time the code is run.
How it should work:
Example - the user inputs 5 seconds.
Second 5, the thread sleeps for .4 seconds.
Second 4, the thread sleeps for .6 seconds.
Second 3, the thread sleeps for 1 seconds.
Second 2, the thread sleeps for 1.2 seconds.
Second 1, the thread sleeps for 1.8 seconds.
So even though the outputs slow down, in total the timer ends after 5 seconds. Let me know if this is confusing!
Here's my code:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
double time = 0; /*used for calculating delay
bc sleep isn't always accurate so
essentially I want to double check if
the sleep function is delaying for the
amount of time I want it to */
Scanner input = new Scanner(System.in);
System.out.print("Enter time: ");
double timerSeconds = input.nextDouble();
for (int timeElapsed = (int)timerSeconds; timerSeconds > 0; timerSeconds--) {
//timeElapsed is placeholder to avoid errors
long t1 = System.nanoTime(); //gets current System time
/* 5 divides, the timer delay increases as the
timerSeconds decreases, but timer should run for
user input in the end*/
if((timerSeconds >= (timerSeconds/5)*4) && !(timerSeconds <= (timerSeconds/5)*3)){
try {
Thread.sleep(400);
} catch (Exception e) {//catches random exception to avoid crash
System.out.println(e);
}
time = (System.nanoTime() - t1)/1000000; //calculates delay
}
if((timerSeconds >= (timerSeconds/5)*3) && !(timerSeconds <= (timerSeconds/5)*2)&& !(timerSeconds >= (timerSeconds/5)*4)){
try {
Thread.sleep(600);
} catch (Exception e) {
System.out.println(e);
}
time = (System.nanoTime() - t1)/1000000;
}
if((timerSeconds >= (timerSeconds/5)*2) && !(timerSeconds <= (timerSeconds/5)*1)&& !(timerSeconds >= (timerSeconds/5)*3)){
try {
Thread.sleep(1000);
} catch (Exception e) {
System.out.println(e);
}
time = (System.nanoTime() - t1)/1000000;
}
if((timerSeconds >= (timerSeconds/5)) && !(timerSeconds <= (timerSeconds/5))&& !(timerSeconds >= (timerSeconds/5)*2)){
try {
Thread.sleep(1200);
} catch (Exception e) {
System.out.println(e);
}
time = (System.nanoTime() - t1)/1000000;
}
if((timerSeconds >0) && !(timerSeconds >= (timerSeconds/5))){
try {
Thread.sleep(1800);
} catch (Exception e) {
System.out.println(e);
}
time = (System.nanoTime() - t1)/1000000;
}
System.out.println("\n" + timerSeconds + " Seconds Remaining");
System.out.println("delay) "+ time);
}
System.out.println("\n\n -----Timer is Up-----\n\n");
}
}
Aucun commentaire:
Enregistrer un commentaire