Java 我的程序继续运行相同的if语句,即使我更新了它正在检查的变量
我正在尝试为计时器编写一个程序,在这个程序中,每次计时器前进1/5,更新输出所需的时间就会更长。我使用了一个sleep线程和5个if语句完成了他的工作,在这些语句中,每当timerSeconds变量下降1/5,它就会降低输出速度。然而,我的if语句似乎不起作用,我每次都得到同样的延迟。有人能看出我做错了什么吗?谢谢 注意:我已经多次重写了我的if语句,并且我根据5个间隔的模型编写了我的延迟,其中总延迟加起来应该是5秒 我还包括了一个延迟函数,它输出睡眠线程的延迟,这纯粹是用来测试系统的计时是否准确 非常感谢你的帮助 问题:Java 我的程序继续运行相同的if语句,即使我更新了它正在检查的变量,java,for-loop,if-statement,nested,thread-sleep,Java,For Loop,If Statement,Nested,Thread Sleep,我正在尝试为计时器编写一个程序,在这个程序中,每次计时器前进1/5,更新输出所需的时间就会更长。我使用了一个sleep线程和5个if语句完成了他的工作,在这些语句中,每当timerSeconds变量下降1/5,它就会降低输出速度。然而,我的if语句似乎不起作用,我每次都得到同样的延迟。有人能看出我做错了什么吗?谢谢 注意:我已经多次重写了我的if语句,并且我根据5个间隔的模型编写了我的延迟,其中总延迟加起来应该是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");
}
}
如您所见,如果运行我的代码,每次运行代码时的延迟为.4秒
它应该如何工作:
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");
}
}
示例-用户输入5秒
第二个5,线程休眠0.4秒
第二个4,线程休眠0.6秒
第二个3,线程休眠1秒
第二个2,线程休眠1.2秒
第二个1,线程休眠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");
}
}
import java.util.Scanner;
班长{
公共静态void main(字符串[]args){
双倍时间=0;/*用于计算延迟
bc睡眠并不总是准确的,所以
基本上我想再检查一下
睡眠功能延迟了一段时间
我想要的时间*/
扫描仪输入=新扫描仪(System.in);
系统输出打印(“输入时间:”);
double timerSeconds=input.nextDouble();
对于(int timeappeased=(int)timerSeconds;timerSeconds>0;timerSeconds--){
//timeappeased是一个占位符,用于避免错误
long t1=System.nanoTime();//获取当前系统时间
/*5分,计时器延迟随着
timerSeconds减小,但计时器应运行一个小时
最终用户输入*/
如果((timerSeconds>=(timerSeconds/5)*4)和&!(timerSeconds=(timerSeconds/5)*3)和&!(timerSeconds=(timerSeconds/5)*4)){
试一试{
睡眠(600);
}捕获(例外e){
系统输出打印ln(e);
}
时间=(System.nanoTime()-t1)/1000000;
}
如果((timerSeconds>=(timerSeconds/5)*2)和&!(timerSeconds=(timerSeconds/5)*3)){
试一试{
睡眠(1000);
}捕获(例外e){
系统输出打印ln(e);
}
时间=(System.nanoTime()-t1)/1000000;
}
如果((timerSeconds>=(timerSeconds/5))&&!(timerSeconds=(timerSeconds/5)*2)){
试一试{
睡眠(1200);
}捕获(例外e){
系统输出打印ln(e);
}
时间=(System.nanoTime()-t1)/1000000;
}
如果((timerSeconds>0)和&!(timerSeconds>=(timerSeconds/5))){
试一试{
睡眠(1800);
}捕获(例外e){
系统输出打印ln(e);
}
时间=(System.nanoTime()-t1)/1000000;
}
System.out.println(“\n”+时间秒+剩余“秒”);
系统输出打印项次(“延迟)”+时间);
}
System.out.println(“\n\n------计时器已启动------\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");
}
}
import java.util.Scanner;
班长{
公共静态void main(字符串[]args){
双倍时间=0;/*用于计算延迟
bc睡眠并不总是准确的,所以
基本上我想再检查一下
睡眠功能延迟了一段时间
我想要的时间*/
扫描仪输入=新扫描仪(System.in);
系统输出打印(“输入时间:”);
double timerSeconds=input.nextDouble();
double timerSecondsFifth=timerSeconds/5;
int timesRan=0;
对于(int timeappeased=(int)timerSeconds;timerSeconds>0;timerSeconds--){
//timeappeased是一个占位符,用于避免错误
long t1=System.nanoTime();//获取当前系统时间
/*5分,计时器延迟随着
timerSeconds减小,但计时器应运行一个小时
最终用户输入*/
如果(timerSeconds>timerSecondsFifth*4&&timerSeconds timerSeconds Fifth*3&&timerSeconds timerSeconds Fifth*2&&timerSeconds timerSeconds Fifth&&timerSeconds如果您需要这么多try捕获,只需通过抛出异常来简化它。“如果这让我感到困惑,请告诉我!”--这让人困惑。@DaveNewton你能告诉我我可以澄清哪一部分吗?@ThomasJadallah Dunno,太让人困惑了。我先检查一下你的if
语句中的变量和逻辑条件;它们显然是错误的,但很难对代码进行推理。仅仅更新一个计数器不是会容易得多吗在每个计时器到期时,使用该计数器设置下一个延迟?@DaveNewton这是个好主意,我会尝试的,很高兴你解决了。对我来说,这仍然是不必要的复杂。