Java代码适用于许多输入,但一个输入除外:500,000(固定)]]

问题描述 投票:-3回答:2

我正在尝试制作时间计算器。 (较早发布了一篇帖子,我已修复)。该代码按预期工作,但当我输入数字500,000时除外。它只是在没有打印语句的情况下自动终止程序。但是,如果愿意,我可以输入1-488888和544444-900000之间的任何内容,它将起作用。

我已经移动了if语句,在这里和那里嵌套了一些。我无法判断if / else if语句中问题出在哪里。


final int x = 9;
final int y = 1;
final int Days;
final int Hours;
final int Minutes;
final int Seconds;

Days = total_seconds / 86400;
Hours = (total_seconds % 86400 ) / 3600;
Minutes = ((total_seconds % 86400 ) % 3600 ) / 60;
Seconds = ((total_seconds % 86400 ) % 3600 ) % 60;

if (Hours == 0) {

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

    System.out.print("You entered " + total_seconds + " second(s), which is " + Minutes + " minute(s), and " +  Seconds + " second(s).");
    System.out.print("\n");
    System.out.print(padded + ":" + padded2);
    }
}
else if (Hours >= y && Days == 0) {
    if (Minutes < x || Seconds < x) {
    String padded = String.format("%02d" , Minutes);
    String padded2 = String.format("%02d" , Seconds);

    System.out.print("You entered " + total_seconds + " second(s), which is " + Hours + " hour(s), " + Minutes + " minute(s), and " +  Seconds + " second(s).");
    System.out.print("\n");
    System.out.print(Hours + ":" + padded + ":" + padded2 + " hours.");
    }
}

else if (Days >= y && Hours >= y) {
    if (Minutes < x || Seconds < x) {
    String padded = String.format("%02d" , Minutes);
    String padded2 = String.format("%02d" , Seconds);

    System.out.print("You entered " + total_seconds + " second(s), which is " + 
    Days + " day(s), " + Hours + " hour(s), " + padded + " minute(s), and " + 
    padded2 + " second(s).");
    System.out.print("\n");
    System.out.print(Days + " day(s) " + Hours + ":" + padded + ":" + padded2 + 
    " hour(s).");
    }
}

else {

System.out.print("You entered " + total_seconds + " second(s), which is " + Days + " day(s), " + Hours + " hour(s), " + Minutes + " minute(s), and " + Seconds + " second(s).");
System.out.print("\n");
System.out.print(Days + " day(s) " + Hours + ":" + Minutes + ":" + Seconds + " hour(s).");
}

当我输入具有天数的任何数字时,它应该输出:

“您输入了500,000秒,即5天18小时53分钟20秒。(5天18:53:20小时)”

实际输出(当我输入500,000时,绝对没有。该程序只是自动终止而不会打印。

编辑:添加了其他内容。问题仍然存在。

我正在尝试制作时间计算器。 (较早发布了一篇帖子,我已修复)。该代码按预期工作,但当我输入数字500,000时除外。它只是自动终止...

java
2个回答
0
投票

Originally,您的程序不输出任何内容,因为您仅在(Minutes < x || Seconds < x)时才打印数据(如果其中一个小于9 ...,它实际上应小于或等于


1
投票

我不知道您的代码有什么问题,但是我认为您应该在此处使用模数,而不是String#format

© www.soinside.com 2019 - 2024. All rights reserved.