私はJavaコースの解決には問題がありますが、私はこの言語を使い慣れていないため、解決策を見つけることができません。誰かが私を助けることができたら嬉しいよ。変数の定義とスイッチの使用
H
は、2.5
のような10進値を含む可能性があるため、double
と定義する必要があります。
現在のコードは完全に動作しますが、H
の整数のみで動作します。私はdouble H = 2.5;
で試しましたが、すべてがうんざりです。また、私はintからdoubleへ、あるいはその逆にスイッチを読み、いくつかのコードを試しましたが、スイッチを使用すると、P1 * H
とP2 * H
が壊れます。
public class PoolTask {
public static void main(String[] args) {
int V = 200;
int P1 = 150;
int P2 = 170;
int H = 2;
int P1w = P1 * H;
int P2w = P2 * H;
int Pt = P1w + P2w;
if (Pt <= V) {
int percentage1 = (Pt * 100/V);
int percentage2 = (P1w * 100/Pt);
int percentage3 = (P2w * 100/Pt);
System.out.println("The pool is " + percentage1 + "% full. Pipe 1: " + percentage2 + "%. Pipe 2: " + percentage3 + "%.");
} else if (Pt > V) {
int liters = (Pt - V);
System.out.println("For " + H + " hours the pool overflows with " + liters + " liters");
} else {
System.out.println("");
}
}
}
変更あなたの変数: 'H'、 'P1w'、' P2w'、 'Pt'、' percentage1'、 'percentage2'と' percentage3'にします「ダブル」。 – DimaSan
と 'liters'も同様 –
[可能な二重からintへの可逆変換]の可能な複製(http://stackoverflow.com/questions/26413265/possible-lossy-conversion-from-double-to-int) – DimaSan