ana ana - 6 months ago 23
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

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

/*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);

}
}

Answer

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.

ALSO,

You need to initiailize your variables or the compilers will show

variable CUpdatedB might not have been initialized
Comments