简体   繁体   English

不需要的 output 用于我的 Java 代码“if 语句”(太长,无法在标题中描述)

[英]Unwanted output for my Java code “if statement” (too long to describe in title)

Edit: added more code编辑:添加更多代码

(extremely new to java, and I don't have any textbooks. I am piecing together how it works from what I can find online, please be nice.) (对 java 来说非常新,而且我没有任何教科书。我正在根据我在网上找到的内容拼凑它的工作原理,请善待。)

I am trying to make a "time calculator" for my Java course in college.我正在尝试为我在大学的 Java 课程制作一个“时间计算器”。 (It's our assignment). (这是我们的任务)。 I have to make the output like so: (days) HH:MM:SS.我必须像这样制作 output:(天)HH:MM:SS。 If there are no Days, then it would be HH:MM:SS, and if no Hours, then it would be MM:SS, and so on.如果没有 Days,则为 HH:MM:SS,如果没有 Hours,则为 MM:SS,依此类推。

However, in my code, my if statement for there being only seconds and no minutes or hours or days is not executing properly.但是,在我的代码中,我的 if 语句只有几秒钟,没有几分钟或几小时或几天没有正确执行。 If i enter 40 seconds, it will output: 0:00:40 hours.如果我输入 40 秒,它将 output: 0:00:40 小时。 How do I fix this?我该如何解决? I know there is a problem with the If/If else statements.我知道 If/If else 语句有问题。

I have tried to figure out the problem by entering various numbers from 1-60, but it always returns as "0:00:(1-60) hours."我试图通过输入 1-60 的各种数字来解决问题,但它总是返回“0:00:(1-60) 小时”。 I don't know where I'd begin to solve this problem.我不知道从哪里开始解决这个问题。

final int x = 9;
final int n_Days;
final int n_Hours;
final int n_Minutes;
final int n_Seconds;

n_Days = total_seconds / 86400;
n_Hours = (total_seconds % 86400 ) / 3600;
n_Minutes = ((total_seconds % 86400 ) % 3600 ) / 60;
n_Seconds = ((total_seconds % 86400 ) % 3600 ) % 60;

if (n_Days == 0) {

    if (n_Minutes < x || n_Seconds < x) {
    String padded = String.format("%02d" , n_Minutes);
    String padded2 = String.format("%02d" , n_Seconds);

    System.out.print("You entered " + total_seconds + " seconds, which is " + n_Hours + " hours, " + n_Minutes + " minutes, and " +  n_Seconds + " seconds.");
    System.out.print("\n");
    System.out.print(n_Hours + ":" + padded + ":" + padded2 + " hours.");
    }
}
else if (n_Days == 0 && n_Hours == 0) {

    if (n_Minutes < x || n_Seconds < x) {
    String padded = String.format("%02d" , n_Minutes);
    String padded2 = String.format("%02d" , n_Seconds);

    System.out.print("You entered " + total_seconds + " seconds, which is " + n_Minutes + " minutes, and " +  n_Seconds + " seconds.");
    System.out.print("\n");
    System.out.print(padded + ":" + padded2);
        }
}

else if (n_Days == 0 && n_Hours == 0 && n_Minutes == 0) {


    System.out.print("You entered " + total_seconds + " seconds, which is " +  n_Seconds + " seconds.");
    }

else {
    if (n_Minutes < x || n_Seconds < x) {
    String padded = String.format("%02d" , n_Minutes);
    String padded2 = String.format("%02d" , n_Seconds);

    System.out.print("You entered " + total_seconds + " seconds, which is " + n_Days + " days, " + n_Hours + " hours, " + padded + " minutes, and " + padded2 + " seconds.");
    System.out.print("\n");
    System.out.print(n_Days + " days " + n_Hours + ":" + padded + ":" + padded2 + " hours.");
    }
}

If I input less than 60 as the total_seconds, then I want the 2nd "else-if" statement to execute like so:如果我输入的总秒数小于 60,那么我希望第二个“else-if”语句像这样执行:

"You entered 30 seconds, which is 30 seconds." “你输入了30秒,也就是30秒。”

if (n_Days == 0)

This is your first if -statement.这是您的第一个if语句。 If n_Days is 0 , it will be executed and - most importantly - the else -statement never will.如果n_Days0 ,它将被执行,并且 - 最重要的是 - else语句永远不会执行。 So if n_Days is 0 and n_Hours is 0 too, if (n_Days == 0) will be executed and the else if... -statements will just be skipped.因此,如果n_Days0n_Hours也为0if (n_Days == 0)将被执行,而else if...语句将被跳过。

You just got to change the order of your statements - or nest them - and will be fine.您只需更改语句的顺序 - 或嵌套它们 - 就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM