2016-05-02 14 views
0

これは非常に簡単なことですが、申し訳ありませんが、この問題を解決するには問題があります。ループ処理をしてwhileのループを削除すると機能しますが、私はwhileループで何が間違っているのか分かりません。助言がありますか?あなたのコードでwhileループで問題が発生しました

/*Cobalt 

60, a radioactive form of cobalt used in cancer therapy, decays or 
dissipates over a period of time. Each year, 12 percent of the 
amount present at the beginning of the year will have decayed. If 
a container of cobalt 60 initially contains 10 grams, create a 
Java program to determine the amount remaining after five years. */ 
public class Cobalt { 

    public static void main(String[] args) { 

     //dec 
     double CInitial = 10.0; 
     double decay = .12; 
     double CUpdatedA, CUpdatedB; 

     //proc 
     int years = 0; 

     while (years < 5); 
     { 

      CUpdatedA = CInitial * decay; 
      CUpdatedB = CInitial - CUpdatedA; 
      years++; 

     } 

     //out   
     System.out.println("the amount of cobalt left after 5 years is" 
       + CUpdatedB); 

    } 
} 
+7

whileループの後に ';'が付きます。つまり、何もしません。 – Natecat

+0

あなたの 'while'ボディは' year'をインクリメントしながら同じ値を繰り返し計算します。 – ArcSine

+0

@ArcSineいいえ、 'year'をインクリメントする部分は@Natecatによって指摘されているように' while'ボディではありません。 – MikeCAT

答えて

1

、この行をよくお読み:

while (years < 5); 

この文はを終了したことを意味終わりにセミコロンがあります。

「ブラケットでエラーが発生しないのはなぜですか?」 角括弧はセクションを意味し、コードには影響しません。

この作業を行う方法は、コロンを削除することです。 ALSO

の除去、すなわちwhileループの問題、ほか

+0

正しい結果はまだ返されませんが、 –

0

(CUpdatedA、CUpdatedB = 0を書く)あなたの変数をinitiailizeする必要があるか、コンパイラが

variable CUpdatedB might not have been initialized 

が表示されますセミコロン。すべてのループが終了すると、CInitialはその年の崩壊後の値で更新されないため、正解を得ていないようです。

ここでは、whileループの最後のステートメントとしてCUpdatedBを使用してCInitialをリセットしています。

public class Cobalt { 

public static void main(String[] args) { 

    //dec 
    double CInitial = 10.0; 
    double decay = 0.12; 
    double CUpdatedA = 0, CUpdatedB = 0; 

    //proc 
    int years = 0; 

    while (years < 5) 
    { 

     CUpdatedA = CInitial * decay; 
     CUpdatedB = CInitial - CUpdatedA; 
     CInitial = CUpdatedB; 
     years++; 

    } 

    //out 
    System.out.println("the amount of cobalt left after 5 years is: " + CUpdatedB); 

} 
} 

出力:5年後に残ったコバルトの量は次のとおりです。5.277319168 私はあなたの期待の答えのthatsを願っています。

関連する問題