0

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 whileloop. 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);

    }
}
4
  • 7
    Your while loop has a ; after it, meaning it does nothing. Commented May 2, 2016 at 23:31
  • Your while body computes the same value over and over, while incrementing year. Commented May 2, 2016 at 23:33
  • @ArcSine No, the part that incrementing year is not the while body, as pointed out by @Natecat. Commented May 2, 2016 at 23:41
  • 1
    I understand the while loop isn't running properly, but once the semicolon is removed, CUpdatedA and CUpdatedB will get the same value on every iteration because they are dependent on variables that are loop invariant. Commented May 2, 2016 at 23:43

2 Answers 2

1

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

(write CUpdatedA, CUpdatedB = 0)

Sign up to request clarification or add additional context in comments.

Comments

0

Besides, the while loop issue i.e, removing of semicolon. You don't seem to be getting correct answer because at the end of every loop your CInitial is not updated with the value after decay during that year.

Here am resetting CInitial with CUpdatedB as a last statement in while loop.

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

}
}

Output: the amount of cobalt left after 5 years is: 5.277319168 I hope thats the answer your expecting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.