Ниже приведен весь исходный код программы из файла Help3.java. /* Пример для опробования 3.3. Завершенная справочная система по управляющим операторам Java, обрабатывающая многократные запросы. */ class Не1рЗ { public static void main(String args[]) throws java.io.IOException { char choice, ignore; for(;;) { do { System.out.println("Help on:"); System.out.println(" 1. if"); System.out.println(" 2. switch"); System.out.println(" 3. for"); System.out.println(" 4. while"); System.out.println(" 5. do-while"); System.out.println(" 6. break"); System.out.println(" 7. continue\n"); System.out.print("Choose one (q to quit): "); choice = (char) System.in.read; do { ignore = (char) System.in.read; } while(ignore != '\n'); } while( choice < '1' | choice > '7' & choice != 'q'); if (choice == 'q') break; System.out.println("\n"); switch(choice) { case '1' : System.out.println("The if:\n"); System.out.println("if(condition) statement;"); System.out.println("else statement;"); break; case '2': System.out.println("The switch:\n"); System.out.println("switch(expression) {"); System.out.println(" case constant:"); System.out.println(" statement sequence"); System.out.println(" break;"); System, out .println (11 // ..."); System.out.println("}"); break; case '3' : System.out.println("The for:\n"); System.out.print("for(init; condition; iteration)"); System.out.println(" statement;"); break; case '4' : System.out.println("The while:\n"); System.out.println("while(condition) statement;"); break; case '5': System.out.println("The do-while:\n"); System.out.println("do {"); System.out.println(" statement;"); System.out.println("} while (condition);"); break; case '6' : System.out.println("The break:\n"); System.out.println("break; or break label;"); break; case '7': System.out.println("The continue:\n"); System.out.println("continue; or continue label;"); break; } System.out.println; } } }
Ниже приведен один из возможных вариантов выполнения данной программы в диалоговом режиме. Help on: 1. if 2. switch 3. for 4. while 5. do-while 6. break 7. Continue Choose one (q to quit): 1 The if: if(condition) statement; else statement; Help on: 1. if 2. switch 3. for 4. while 5. do-while 6. break 7. Continue Choose one (q to quit): 6 The break: break; or break label; Help on: 1. if 2. switch 3. for 4. while 5. do-while 6. break 7. Continue Choose one (q to quit): q Вложенные циклы
Как следует из предыдущих примеров программ, один цикл может быть вложен в другой. С помощью вложенных циклов решаются самые разные задачи. Поэтому, прежде чем завершить рассмотрение циклов в Java, уделим еще немного внимания вложенным циклам. Ниже приведен пример программы, содержащей вложенные циклы for. С помощью этих циклов для каждого числа от 2 до 100 определяется ряд множителей, на которые данное число делится без остатка. /* Использовать вложенные циклы для выявления множителей чисел от 2 до 100. */ class FindFac { public static void main(String args[]) { for(int i=2; i <= 100; i++) { System.out.print("Factors of " + i + ": ") ; for(int j = 2; j < i; j++) if((i%j) == 0) System.out.print(j + " "); System.out.println ; } } }