-6
class IfElse
{
    public static void main (String[] args) throws java.lang.Exception
    {
        double x = 1784;

        while (x <= 1){

            if((x % 2) = 0){

                x = x / 2;
            }
            else{
                x = 3 * x + 1;
            }
            System.out.println(x);
        }
    }
}

well, I am having problems with the % (modulus). Could someone help? I am a begginer at coding. Have no idea what I am doing. If someone could help´with this...

2 Answers2

0

There's a couple issues:

  1. Your while loop (since x is initially set to 1784, but your while loop only runs while x is less than or equal to 1. I think you're going to want to swap those two values around, assuming you want to start at 1 and end when it hits or exceeds 1784.

  2. Additionally, when you're evaluating equality between two values you need to use == rather than a single =. One = assigns a value, the == does an equality comparison and returns a true or false.

Here's additional resources on the equality & relational operators in Java: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html As well as information on loop control in Java: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

0

First off your loop will never be entered because 1784 is not less than or equal to 1. Also you need to check equality using == not =. Additionally word of advice, it is strongly recommended that you not use doubles for comparison as rounding can lead to unpredictable results. I would advice you to change double x to int x, unless you are required to use a double.

Greg Gardner
  • 190
  • 11