hi, i dont want it to seem like everytime i have a problem i come to this forum...but i just need to cover all bases b4 my exam in a month...thanks for understanding.

my question is..is there any way to write the following program WITHOUT using a while loop? i just want to use if and else in place of it. when i write my own code...it's filled with errors. i think it's because i mostly use "techniques" from c++.

public class ThreeN {
/* This program prints out a 3N+1 sequence
starting from a positive integer specified
by the user. It also counts the number
of terms in the sequence, and prints out
that number. */
public static void main(String[] args) {
int N; // for computing terms in the sequence
int counter; // for counting the terms
TextIO.put("Starting point for sequence: ");
N = TextIO.getlnInt();
while (N <= 0) {
TextIO.put("The starting point must be positive. "
+ " Please try again: ");
N = TextIO.getlnInt();
// At this point, we know that N > 0
counter = 0;
while (N != 1) {
if (N % 2 == 0)
N = N / 2;
N = 3 * N + 1;
counter = counter + 1;
TextIO.put("There were ");
TextIO.putln(" terms in the sequence.");
} // end of main()
} // end of class ThreeN

Thanks for all the help!!!

I'll post what i wrote in a while..i've editted it so many times it doesnt make sense to me anymore.

10 Years
Discussion Span
Last Post by mikki2
public class Banking
    public static void main(String[] args)
            int N;
            int userInput;
            TextIO.put("Please input a number: ");
            userInput = TextIO.getlnInt();
            if ( userInput%2 == 1)
            N = N/2; 
                TextIO.putln("Output is " + N);
            else if (userInput%2 == 0)
                TextIO.putln("Output is " + N);

    } // end of main()
} // end of class

so wrong!!!


me again( basically talking to myself since no one answered..lol)! i tried all night and couldn't find a way to write the above program(and make it work properly) without a while , so i'll just stick with the it.
Thanks for letting me post on your site :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.