Think Java(http://www.greenteapress.com/thinkapjava/thinkapjava.pdf)の23ページのエクササイズ2.3を行っています。正しいパーセンテージを計算するためにdoubleを代わずに変数をintとして表現する方法はありますか?
変数に格納されている任意の時間を秒に変換してから、その日に残っている秒数を計算し、その日のどれだけ経過したかをパーセンテージで計算します。
ここでの作業プログラムです:
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
}
}
は、私は、より高度なコードとのより良い方法がある知っているが、これは割り当ては、我々がこれまでに学んだことに基づいて、必要なものです。しかし、私は倍精度浮動小数点数型をトリミングするために書式指定子を使用しなければならないので、変数を表現する最善の方法は二重であるとは思わない。私はそれについての私の教授に尋ねた、彼は私がint型との最後のprint文で計算を変更するには、すべての変数を変更することができると言いました:!
System.out.printf("The percentage of the day that has passed is: %d%%", (100 * SEC_SINCE_MN) * 1.0/SEC_IN_DAY);
私は広告を取得するので、これは動作しません= java.lang.Doubleの最後のprintステートメントのエラー。私が1.0を1に変更した場合、最後の出力が間違っているというエラーは表示されません。
最後のprint文の出力の小数点の値が0.08788であるため、これは正しい出力である88%ではなく87%と言います。
私はそれがintで動作するように変更する必要があると思います。
doubleの代わりにintのプログラムを編集する方法はありますか?
EDIT 1:コード私の教授の提案どおりに動作しません(最後のprint文のため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
}
}
EDIT 2:コードが、doesnの作品」 tは最後のprint文
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
}
}
EDIT 3用に88%の正しい出力を与える:これはだまされやすい人の質問ではありません。私の質問は、倍精度からintに変数を変換する方法だったので、パーセンテージは最後のステートメントで正しく計算されました。私はそれが質問/回答に関与していましたが、丸めを求めていませんでした。
だから、ダブルを丸める方法を聞いていますよね? https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#round-double- –
@JB Nizet、いいえ。私のプログラムは私に正しい出力を与えます。すべての目的と目的のために、intの代わりにdouble変数を使用して割り当てを正常に完了しました。しかし、変数は代わりにintでなければなりませんが、変数がintのときに最後のステートメントを動作させる方法はわかりません。 – srocode
動作しないコードを表示するのではなく、動作するコードを表示するのはどうでしょうか?起こると予想されることを正確に教えてください。代わりに何が起こりますか?しかし、 '(100 * SEC_SINCE_MN)* 1.0/SEC_IN_DAY'は二重で、丸めたいと思う。 –