Having trouble with a while loop that gets a certain series of numbers from an input

JamesR

I'm trying to half the input if it % 12 == 0, but if it isn't then you multiply it by 3 and add 1 onto the sum.

The question that I'm working off is: http://i.imgur.com/VzuPtZJ.png

enter image description here

With the code I have currently(which is below), if I enter 12, like in the question I start off with 6, but then the results begin to go wrong and then they go insanely wrong with values in the millions and negative millions etc.

import java.util.*;
public class sheet12t3
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int aNumber = Integer.parseInt(in.nextLine());
        hailstone(aNumber);
    }

    public static void hailstone(int x)
    {
        int count = 1;
        int max = 0;
        String results = "Hailstone series for the number " + x + " is ";
        while (x >= 1)
        {
            if (x % 12 == 0)
                x = x / 2;
            else
                x = 3 * x + 1;

            count++;

            results += + x + ", ";

            if (x > max)
                max = x;
        }
        results += "a total of " + count + " numbers in this sequence with a maximum value of " + max;
        System.out.print(results);
    }
}
Salih Erikci

The question says divide by two if the number is even. But you divide by 2 only when it is dividable by 12.

Change this line

(x % 12 == 0)

to

(x % 2 == 0)

And change while (x >= 1) to while (x > 1)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Having trouble summing while loop values with awk

From Dev

How to use a while loop to enter user input into an array while keeping certain numbers unchanged

From Dev

Having trouble extracting data from loop

From Dev

Having Trouble Making a for loop with a do-while loop

From Dev

Having trouble with limiting the input of a textbox to numbers C#

From Dev

HAVING skipping certain numbers from an alias

From Dev

Having trouble with while loop in my number guessing game

From Dev

Having trouble with a while loop, it breaks, even though it shouldn't

From Dev

Having trouble with using a while loop for simulating a 1 in 5 chance

From Dev

Having trouble writing numbers from C# to SQL database

From Dev

Having trouble writing numbers from C# to SQL database

From Dev

How do I reverse the order of a series of numbers that are generated from a while loop?

From Dev

I am having trouble with making a variable from an input on an Entry Box

From Dev

Having trouble passing a value from one input to another with jquery

From Dev

Having trouble for getting input from EditText field for Android

From Dev

Scanner input gets appended to textfile despite condition in while-loop

From Dev

Having trouble requesting input in python

From Dev

Trouble using a series of getlines to extract elements from user input

From Dev

I'm having trouble returning a certain output based on the number I input into the textfield

From Dev

having trouble with my JavaScript for loop

From Dev

Having Paint loop trouble in java

From Dev

Having Paint loop trouble in java

From Dev

having trouble with my JavaScript for loop

From Dev

Having Trouble executing function but not for loop it is in

From Dev

Having trouble with count controlled for loop

From Dev

Storing input from a while loop outside the loop

From Dev

trouble with while loop java

From Dev

Blackjack While Loop Trouble

From Dev

While loop gets ignored

Related Related

  1. 1

    Having trouble summing while loop values with awk

  2. 2

    How to use a while loop to enter user input into an array while keeping certain numbers unchanged

  3. 3

    Having trouble extracting data from loop

  4. 4

    Having Trouble Making a for loop with a do-while loop

  5. 5

    Having trouble with limiting the input of a textbox to numbers C#

  6. 6

    HAVING skipping certain numbers from an alias

  7. 7

    Having trouble with while loop in my number guessing game

  8. 8

    Having trouble with a while loop, it breaks, even though it shouldn't

  9. 9

    Having trouble with using a while loop for simulating a 1 in 5 chance

  10. 10

    Having trouble writing numbers from C# to SQL database

  11. 11

    Having trouble writing numbers from C# to SQL database

  12. 12

    How do I reverse the order of a series of numbers that are generated from a while loop?

  13. 13

    I am having trouble with making a variable from an input on an Entry Box

  14. 14

    Having trouble passing a value from one input to another with jquery

  15. 15

    Having trouble for getting input from EditText field for Android

  16. 16

    Scanner input gets appended to textfile despite condition in while-loop

  17. 17

    Having trouble requesting input in python

  18. 18

    Trouble using a series of getlines to extract elements from user input

  19. 19

    I'm having trouble returning a certain output based on the number I input into the textfield

  20. 20

    having trouble with my JavaScript for loop

  21. 21

    Having Paint loop trouble in java

  22. 22

    Having Paint loop trouble in java

  23. 23

    having trouble with my JavaScript for loop

  24. 24

    Having Trouble executing function but not for loop it is in

  25. 25

    Having trouble with count controlled for loop

  26. 26

    Storing input from a while loop outside the loop

  27. 27

    trouble with while loop java

  28. 28

    Blackjack While Loop Trouble

  29. 29

    While loop gets ignored

HotTag

Archive