如何解决即使我更新了它要检查的变量,我的程序仍继续运行相同的if语句
我正在尝试编写一个计时器程序,其中每当计时器运行1/5时,更新输出所花费的时间就会更长。我使用了一个睡眠线程和5条if语句来完成他的操作,其中,每当timerSeconds变量下降1/5时,它都会减慢输出速度。但是,我的if语句似乎不起作用,并且每次都遇到相同的延迟。谁能看到我做错了吗?谢谢!
注意:我已经多次重写了if语句,并基于5个间隔的模型编写了延迟,其中总延迟总计为5秒。
我还包括了一个延迟功能,该功能可以输出睡眠线程的延迟,这纯粹是用来测试系统的计时是否准确。
非常感谢您的帮助!
问题:
如您所见,如果您运行我的代码,则每次运行代码的延迟为0.4秒。
应如何工作:
示例-用户输入5秒。
第二个,线程睡眠了0.4秒。
第二个,线程休眠0.6秒。
第二个,线程休眠1秒。
第二个,线程休眠1.2秒。
第二个,线程休眠1.8秒。
因此,即使输出速度变慢,总的来说,计时器也会在5秒钟后结束。让我知道这是否令人困惑!
这是我的代码:
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");
}
}
解决方法
我修复了这个问题,本质上是通过重写我的if语句来测试timerSeconds是否低于前五分之一(我将其存储在名为timerSecondsFifth的int中)并且高于后五分之一。如果有人想看,这是我的代码:
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();
double timerSecondsFifth = timerSeconds/5;
int timesRan = 0;
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 > timerSecondsFifth*4 && timerSeconds <= timerSecondsFifth*6){
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 > timerSecondsFifth*3 && timerSeconds <= timerSecondsFifth*4){
try {
Thread.sleep(600);
} catch (Exception e) {
System.out.println(e);
}
time = (System.nanoTime() - t1)/1000000;
}
if(timerSeconds > timerSecondsFifth*2 && timerSeconds <= timerSecondsFifth*3){
try {
Thread.sleep(1000);
} catch (Exception e) {
System.out.println(e);
}
time = (System.nanoTime() - t1)/1000000;
}
if(timerSeconds > timerSecondsFifth && timerSeconds <= timerSecondsFifth*2){
try {
Thread.sleep(1200);
} catch (Exception e) {
System.out.println(e);
}
time = (System.nanoTime() - t1)/1000000;
}
if(timerSeconds <= timerSecondsFifth){
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");
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。