Java 有没有办法将变量表示为int而不是double来计算正确的百分比?

Java 有没有办法将变量表示为int而不是double来计算正确的百分比?,java,time,int,double,Java,Time,Int,Double,我正在做ThinkJava()第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 se

我正在做ThinkJava()第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,把最后一个print语句中的计算改成:

System.out.printf("The percentage of the day that has passed is: %d%%", (100 * SEC_SINCE_MN) * 1.0 / SEC_IN_DAY); 
这不起作用,因为我得了d!=最后一个print语句的java.lang.Double错误。如果将1.0更改为1,则不会出现错误,因为最后的输出不正确

它表示87%,而不是88%,这是正确的输出,因为最后一次打印语句输出的十进制值为0.08788

我认为这是我的计算,需要改变它的工作与int

关于如何编辑int而不是double的程序有什么想法吗

编辑1:不符合我教授建议的代码(返回java.lang.double error作为最后一个print语句)

编辑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) * 1.0 / SEC_IN_DAY); // Escape percent sign is %%. 100 * to remove decimal value

}

}
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:这不是一个重复的问题。我的问题是如何将变量从double转换为int,以便在上一条语句中正确计算百分比。我不是在问舍入,尽管它确实在问题/答案中起了作用。

int抛出句点之后的内容。你可能想再复制10次。获取第一个数字以检查其是否大于5,并在百分比中添加一个数字。 很抱歉没有写代码。 我现在在iOS上,我的java生锈了。。。 祝你好运

将整数SEC_SINCE_MN乘以整数100,然后除以整数SEC_IN_DAY。这是一个整数除法。它只是截断结果的小数部分

您需要的是计算精确的百分比,用小数,然后对结果进行四舍五入。所以你需要一个浮点除法:

(100.0 * SEC_SINCE_MN) / SEC_IN_DAY
这将产生一个双倍值(87.885416666667),然后需要对其进行四舍五入:

Math.round((100.0 * SEC_SINCE_MN) / SEC_IN_DAY)

那么,你是在问如何圆一个双人,对吗@JB Nizet,没有。我的程序按原样提供正确的输出。出于所有意图和目的,我已经成功地使用双变量而不是int完成了赋值。但是,变量应该是int,但我不知道当变量是int时如何使最后一个语句工作。向我们展示不工作的代码,而不是工作的代码如何?准确地告诉我们你期望发生什么,以及会发生什么。但是,
(100*秒)×1.0/SEC\u IN\u DAY
是一个双精度,您需要舍入它。@JBNizet,好的,几分钟后就可以了。谢谢,这给了我我想要的答案(以后会非常有用),但是,它不能满足作业要求,因为它涉及到使用Math.round,这在我的课程中还没有涉及。我将添加它作为替代版本,并研究Math.round,以便解释它是如何工作的以及为什么工作的。如果您知道如何不用Math.round来完成此任务,请告诉我。然后保持
%.0f%%
,并使用
(100.0*秒/天/秒)
。所有的计算部分都是使用整数完成的,除了最后一个需要是双精度的。这就是说,作为一名开发人员,非常、非常、非常重要的一部分在于对某个主题一无所知,阅读文档进行学习,并尝试在课堂上从未学过的东西。你的老师应该很高兴你能够做到自主,并且使用课堂上从未见过的课程。啊,是的,谢谢!这也非常有效,正是我所需要的。现在,就我的理解而言,可以添加100.0来将输出转换为double,这样它就可以打印十进制值,%.0f允许它四舍五入并截断到最接近的整数1,对吗?进行四舍五入,而不是截断。这是一个或另一个。但是是的,你有这个想法。
Math.round((100.0 * SEC_SINCE_MN) / SEC_IN_DAY)