I'm learning java. I suspect my compute_Hailstone_Sequence
method terminates upon hitting a return statement. With the two inputs of begin_num = 7
and steps = 10
my ideal output should represent 7 22 11 34 17 52 26 13 40 20
I also suspect I am not yet incrementing x
as my code DOES produces the first two values 7 22
..I am not sure it can yet produce a cumulative algorithm. I am aware of answers using data structures but I am trying to code this without using lists or arrays or any other data structure. This is not homework.
import java.util.Scanner;
/**
* Created on 9/5/15.
* Following Reges, Stuart, and Martin Stepp. Building Java Programs: A Back to Basics Approach. 3rd Edition.
* Chapter 4
*/
public class Ch4 {
public static void main(String[] args) {
hailstone_Sequence();
}
public static void hailstone_Sequence(){
giveIntro();
Scanner user_console = new Scanner(System.in);
System.out.println("Please provide a starting integer: ");
int BEGINVALUE = user_console.nextInt();
// System.out.println("Please provide an ending integer: ");
// int ENDVALUE = user_console.nextInt();
System.out.println("Thank you. How long would you liked the sequence to be?");
int STEPS = user_console.nextInt();
System.out.println("Calculating..");
compute_Hailstone_Sequence(BEGINVALUE, STEPS);
}
public static int compute_Hailstone_Sequence(int begin_num, int steps){
System.out.print(begin_num + " ");
for (int i = 1; i <= steps; i++ ){
int x;
if ((begin_num & 1) == 0 ){
// even
// int x = 0;
x = (begin_num / 2);
System.out.print(x + " ");
// return x;
}
else {
// int x = 0;
x = (3 * begin_num + 1);
System.out.print(x + " ");
// return x;
}
return x;
}
return begin_num;
}
}