简体   繁体   中英

How do I make a java program take the results from a previous loop and input it in the same process?

So for my programming class I was given the task of writing a program to take a number, and put that number through something called a Hailstone sequence which looks something like this if we were to use the number 15 for example:

15 is odd, so I make 3n+1: 46

46 is even, so I take half: 23

23 is odd, so I make 3n+1: 70

70 is even, so I take half: 35

35 is odd, so I make 3n+1: 106

106 is even, so I take half: 53

53 is odd, so I make 3n+1: 160

160 is even, so I take half: 80

80 is even, so I take half: 40

40 is even, so I take half: 20

20 is even, so I take half: 10

10 is even, so I take half: 5

5 is odd, so I make 3n+1: 16

16 is even, so I take half: 8

8 is even, so I take half: 4

4 is even, so I take half: 2

2 is even, so I take half: 1

So basically, you input a number, the program checks to see whether the number is odd or even, and based on that it puts the number through one of two processes: multiplying the number by 3 and then adding one, or just dividing the number in half until the end result is just 1.

Here is what I have written so far:

public static void main(String[] args) {
    // TODO Auto-generated method stub
        int number;
    System.out.println("Please enter a number:");
    Scanner s = new Scanner (System.in);

    double number1 = s.nextDouble();

    while (number1 > 1){
    if ( number1 % 2 == 0)
        System.out.println(number1 + " is even, so I take half " + (number1 / 2));
    else
        System.out.println(number1 + " is odd, so I make 3n + 1 " + ((3 * number1) + 1));

}

It'll prompt me to enter a number, I'll enter a number and it'll correctly identify whether the number is odd or even and put it through the appropriate formula, but instead of repeating the process with the result given until we reduce the number to 1, it just gets stuck on an infinite loop on the result of the initial number given.

My question is, how do I get my program to take the results of a loop, and take that number and input it into the process and just keep on repeating the process with the next number that comes up and so forth until we reduce it to one. How do I break out from the infinite loop? I apologize for not being able to word this any more clearly, I'm still very new to the Java language.

Any help I could get would be greatly appreciated!!

You have to update the value of number1. So, in the if and else statements, you need to actually apply the operations to the variable. Example:

if ( number1 % 2 == 0)
{
    System.out.println(number1 + " is even, so I take half " + (number1 / 2));
    number1 = number1 / 2;
}
else
{
    System.out.println(number1 + " is odd, so I make 3n + 1 " + ((3 * number1) + 1)
    number1 = (3*number1) + 1;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM