Results 1 to 5 of 5
  1. #1
    Jaxful is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Exclamation Need some help for a school project.

    So, im working on this schoolwork that i was supposed to finish in class today, and im close. We are supposed to use the Collatz sequence to get any random integer above 0 to be 1, and say how many iterations it took, and the highest number there was , but i cannnot figure out how to print the highest number, any help would be appreciated.

    here is my code.

    import java.util.Scanner;
    public class Collatz
    {
    public static void main(String args[] )
    {
    int num;
    int i = 0;
    int max = 0;
    Scanner s = new Scanner (System.in);
    do
    {
    System.out.println("Please enter a starting number above 0: ");
    num = s.nextInt();
    }
    while (0 > num);

    while (num != 1)
    {
    if (num%2 == 0)
    {
    num = (num / 2);
    System.out.println(num);
    i = i + 1;
    }

    else if (num%2 == 1)
    {
    num = (num * 3) + 1;
    System.out.println(num);
    i = i + 1;
    }


    }
    System.out.println("Terminated after " + i + " iterations ");
    if (max < num)
    {
    max = num;
    System.out.println("The highest number is " + max);
    }

    }
    }

  2. #2
    donohoes is offline Member
    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0

    Default Re: Need some help for a school project.

    could you not and another int highNum and then check if (num>highNum){ highNum==num}

  3. #3
    Jaxful is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Default Re: Need some help for a school project.

    Did that at the bottom of the code but im not sure if i did it right...
    if (max < num)
    {
    max = num;
    System.out.println("The highest number is " + max);
    }

  4. #4
    donohoes is offline Member
    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0

    Default Re: Need some help for a school project.

    its should be more inside the while loop so that it actully chages with num

  5. #5
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    263
    Rep Power
    2

    Default Re: Need some help for a school project.

    What donohoes said and also in your do-while loop, the condition 0 > num would cause trouble if 0 is entered, change it to 0 >= num

Similar Threads

  1. Help For School Project!
    By appy in forum New To Java
    Replies: 8
    Last Post: 10-16-2013, 11:37 AM
  2. Need help with school project
    By camaro01 in forum New To Java
    Replies: 16
    Last Post: 02-20-2011, 06:34 PM
  3. School project help!
    By Addez in forum Advanced Java
    Replies: 5
    Last Post: 09-17-2010, 10:37 PM
  4. Need help with school project!
    By jaxxx in forum New To Java
    Replies: 5
    Last Post: 03-25-2010, 08:55 PM
  5. school project help
    By justin8790 in forum New To Java
    Replies: 5
    Last Post: 03-22-2009, 09:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •