【发布时间】:2016-09-17 18:39:05
【问题描述】:
我正在做 Think Java (http://www.greenteapress.com/thinkapjava/thinkapjava.pdf) 第 23 页上的练习 2.3。
程序将存储在变量中的任意时间转换为秒,然后计算一天中剩余的秒数,然后计算一天中已经过去了多少百分比。
这是工作程序:
public class Time {
public static void main(String[] args) {
double hour = 21.0; //Represents 9 PM
double minute = 5.0; //Represents 9:05 PM
double second = 33.0; //Represents 9:05:33 PM
final double SEC_IN_MIN = 60.0; //Made final because the amount of seconds in a minute does not change
final double SEC_IN_HOUR = 3600.0; //Made final for the same reason above.
final double SEC_SINCE_MN = (SEC_IN_HOUR * hour) + (SEC_IN_MIN * minute) + second; //Calculates the seconds since midnight, or 00:00
final double SEC_IN_DAY = 24.0 * SEC_IN_HOUR; //Calculates the amount of seconds in a day; 24 stands for the hours in a day
System.out.printf("The number of seconds since midnight is: %.0f\n", SEC_SINCE_MN);
System.out.printf("The number of seconds remaining in the day is: %.0f\n", SEC_IN_DAY - SEC_SINCE_MN);
System.out.printf("The percentage of the day that has passed is: %.0f%%", (100 * SEC_SINCE_MN) / SEC_IN_DAY); // Escape percent sign is %% 100 * to remove decimal value
}
}
我知道有一种更好的方法可以编写更高级的代码,但根据我们目前所学的知识,这是分配所需的内容。但是,我不确定 double 是表示变量的最佳方式,因为我必须使用格式说明符来修剪小数点。我问了我的教授,他说我可以将所有变量更改为 int 并将最后一个打印语句中的计算更改为:
System.out.printf("The percentage of the day that has passed is: %d%%", (100 * SEC_SINCE_MN) * 1.0 / SEC_IN_DAY);
这不起作用,因为最后一个打印语句出现 d != java.lang.Double 错误。如果我将 1.0 更改为 1,我不会收到任何错误,但最后的输出不正确。
它显示的是 87% 而不是 88%,这是正确的输出,因为最后一个打印语句输出的十进制值为 0.08788。
我认为需要更改我的计算才能使其与 int 一起使用。
关于如何将程序编辑为 int 而不是 double 的任何想法?
编辑 1:无法按照我教授的建议工作的代码(返回最后一个打印语句的 java.lang.double 错误)
public class Time {
public static void main(String[] args) {
int hour = 21; //Represents 9 PM
int minute = 5; //Represents 9:05 PM
int second = 33; //Represents 9:05:33 PM
final int SEC_IN_MIN = 60; //Made final because the amount of seconds in a minute does not change
final int SEC_IN_HOUR = 3600; //Made final for the same reason above.
final int SEC_SINCE_MN = (SEC_IN_HOUR * hour) + (SEC_IN_MIN * minute) + second; //Calculates the seconds since midnight, or 00:00
final int SEC_IN_DAY = 24 * SEC_IN_HOUR; //Calculates the amount of seconds in a day; 24 stands for the hours in a day
System.out.printf("The number of seconds since midnight is: %d\n", SEC_SINCE_MN);
System.out.printf("The number of seconds remaining in the day is: %d\n", SEC_IN_DAY - SEC_SINCE_MN);
System.out.printf("The percentage of the day that has passed is: %d%%", (100 * SEC_SINCE_MN) * 1.0 / SEC_IN_DAY); // Escape percent sign is %%. 100 * to remove decimal value
}
}
编辑 2:有效但没有为最后一个打印语句提供 88% 的正确输出的代码
public class Time {
public static void main(String[] args) {
int hour = 21; //Represents 9 PM
int minute = 5; //Represents 9:05 PM
int second = 33; //Represents 9:05:33 PM
final int SEC_IN_MIN = 60; //Made final because the amount of seconds in a minute does not change
final int SEC_IN_HOUR = 3600; //Made final for the same reason above.
final int SEC_SINCE_MN = (SEC_IN_HOUR * hour) + (SEC_IN_MIN * minute) + second; //Calculates the seconds since midnight, or 00:00
final int SEC_IN_DAY = 24 * SEC_IN_HOUR; //Calculates the amount of seconds in a day; 24 stands for the hours in a day
System.out.printf("The number of seconds since midnight is: %d\n", SEC_SINCE_MN);
System.out.printf("The number of seconds remaining in the day is: %d\n", SEC_IN_DAY - SEC_SINCE_MN);
System.out.printf("The percentage of the day that has passed is: %d%%", (100 * SEC_SINCE_MN) / SEC_IN_DAY); // Escape percent sign is %%. 100 * to remove decimal value
}
}
编辑 3:这不是一个骗人的问题。我的问题是如何将变量从双精度转换为 int,因此在最后一个语句中正确计算了百分比。我问的不是四舍五入,尽管它确实在问题/答案中发挥了作用。
【问题讨论】:
-
所以,您是在问如何舍入双精度数,对吗? docs.oracle.com/javase/8/docs/api/java/lang/…
-
@JB Nizet,没有。我的程序原样给了我正确的输出。出于所有意图和目的,我已经使用双变量而不是 int 成功完成了分配。但是,变量应该是 int,但是当变量是 int 时,我不知道如何使最后一条语句起作用。
-
向我们展示无效的代码,而不是有效的代码如何?准确地告诉我们您期望发生什么,以及会发生什么。但是,
(100 * SEC_SINCE_MN) * 1.0 / SEC_IN_DAY是一个双精度数,您想将其舍入。 -
@JBNizet,好的,一会儿就可以了。