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

时间:2016-09-17 18:39:06

标签: java time int double

我正在进行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并将最后一个print语句中的计算更改为:

System.out.printf("The percentage of the day that has passed is: %d%%", (100 * SEC_SINCE_MN) * 1.0 / SEC_IN_DAY); 

这不起作用,因为我得到了最后一个print语句的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,因此在最后一个语句中正确计算了百分比。我没有询问有关舍入的问题,尽管它确实在问题/答案中起作用。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:-1)

(100 * SEC_SINCE_MN) / SEC_IN_DAY

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

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

(100.0 * SEC_SINCE_MN) / SEC_IN_DAY

那将产生一个双倍值(87.88541666666667),然后你需要进行舍入:

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