0

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;
    }
}
phillipsK
  • 1,466
  • 5
  • 29
  • 43

2 Answers2

1

Try this, I do not know if that's what you are looking for, me are based on your output.

public static void hailstone_Sequence(){
        Scanner user_console = new Scanner(System.in);
        System.out.println("Please provide a starting integer: ");
        int BEGINVALUE = 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 void compute_Hailstone_Sequence(int begin_num, int steps){
        System.out.print(begin_num + " ");
        for (int i = 1; i < steps; i++ ){
            if (begin_num%2 == 0 ){
                begin_num = (begin_num / 2);
                System.out.print(begin_num + " ");
            }
            else {
                begin_num = (3 * begin_num) + 1;
                System.out.print(begin_num + " ");
            }
        }
    }

Output is:

Please provide a starting integer:

7

Thank you. How long would you liked the sequence to be?

10

Calculating..

7 22 11 34 17 52 26 13 40 20

Michele Lacorte
  • 5,323
  • 7
  • 32
  • 54
0
import java.util.Scanner;

/**
 * Created by 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) {
//          expected output = 7 22 11 34 17 52 26 13 40 20 10
        System.out.print(begin_num + " ");
        int x = 0;
        for (int i = 1; i <= steps; i++) {
            if ((begin_num & 1) == 0) {
//                even
//                int x = 0;
                x = (begin_num / 2);
                System.out.print(x + " ");
//                return x;
                begin_num = x;
            } else {
//                int x = 0;
                x = (3 * begin_num + 1);
                System.out.print(x + " ");
//                return x;
                begin_num = x;
            }
        }
        return x;
    }
        public static void giveIntro(){
            System.out.println("In mathematics, there is an open      problem that involves\n " +
            "what are known as hailstone sequences.\n" +
            "These sequences of numbers often rise and\n " +
            "fall in unpredictable pattern, which is somewhat\n" +
            " analogous to the process that forms hailstones.\n");
    System.out.println("This method outputs hailstone sequences:");
}

}

phillipsK
  • 1,466
  • 5
  • 29
  • 43