ana ana - 2 years ago 103
Java Question

trouble with while loop java

This is extremely simple and I'm sorry I am having trouble getting this to work. It works when I remove the

loop with just the processing, but I'm not sure what I'm doing wrong with the
loop. Any suggestions?


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) {

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

int years = 0;

while (years < 5);

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


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


Answer Source

In your code, read this line carefully :

while (years < 5);

There is a semicolon at the end, which means that this statement is finished.

You might ask, "why doesn't the bracket cause an error?" The brackets mean a section, it does not affect the code.

The way to make this work is to remove the colon.


You need to initiailize your variables or the compilers will show

variable CUpdatedB might not have been initialized
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download