I'm a beginner and I only know limited amounts such as if
, while
, do while
. So I'm here to check if I'm coding with best practise and the most effective methods to my current knowledge.
import java.util.Scanner;
public class SixtyTwo {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Starting Number: ");
int n = keyboard.nextInt();
int counter = 0;
int stepsTaken = 0;
int largestNumber = 0;
System.out.println();
while ( n != 1 ){
if ( ( n & 1 ) == 0 ) {
System.out.print( (n = ( n / 2 )) + " " );
stepsTaken++;
counter++;
} else {
System.out.print( (n = ( n * 3 ) + 1) + " " );
stepsTaken++;
counter++;
}
if ( n > largestNumber ){
largestNumber = n;
}
if (counter == 9){
counter = 0;
System.out.print("\n");
}
}
System.out.println();
System.out.println("\nTerminated after " + stepsTaken + " steps.");
System.out.println("The largest value was " + largestNumber + ".");
}
}