01: // plik 06_TryCatch_1/Ciag.java 02: 03: import java.util.*; // Scanner, InputMismatchException 04: 05: public class Ciag { 06: 07: static String NL = System.getProperty("line.separator"); 08: 09: public static void main(String[] args) { 10: 11: Scanner scan = new Scanner(System.in); 12: 13: while(true) { 14: System.out.print("Podaj liczb\u0119 ca\u0142" + 15: "kowit\u0105 wi\u0119ksz\u0105 od jedynk" + 16: "i lub 0 aby sko\u0144czy\u0107: "); 17: 18: int k = 0; 19: try { 20: k = scan.nextInt(); 21: } catch(InputMismatchException e) { 22: System.out.println("To nie liczba!"); 23: scan.skip(".*"); 24: continue; 25: } 26: 27: if ( k == 0 ) System.exit(0); 28: 29: if ( k <= 1 ) { 30: System.out.println("Za ma\u0142a liczba"); 31: continue; 32: } 33: 34: System.out.println("Wartosc startowa: " + k); 35: int cnt = 0; 36: while ( k > 1 ) { 37: if (k%2 == 0) 38: k /= 2; 39: else 40: k = 3*k+1; 41: cnt++; 42: System.out.printf("a[%3d] = %3d%s", 43: cnt, k,NL); 44: } 45: } 46: } 47: }