while (繰返し条件式) { 処理; } |
|
- public class GCD01 {
- public static void main(String[] args) {
- int a = Integer.parseInt(args[0]);
- int b = Integer.parseInt(args[1]);
- int r;
- while ( /* ここにコードを記述してください。*/ ) {
- // ここにコードを記述してください。
- }
- System.out.println(args[0] + " と " + args[1] + " の最大公約数は, " + b + " です。" );
- }
- }
□ 実行結果
$ java GCD01 12 32 12 と 32 の最大公約数は, 4 です。
for (初期化式; 条件式; 更新式) { ループの処理 } 初期化式; while (条件式) { ループ中の処理 更新式; }
- public class For11 {
- public static void main(String[] args) {
- for (int i = 0; i < 10; i++) {
- System.out.println(i);
- }
- }
- }
- public class While11 {
- public static void main(String[] args) {
- int i = 0; // 初期値化式
- while (i < 10) { // ループ条件
- System.out.println(i);
- i++; // 更新式
- }
- }
- }
do { 処理; } while (繰返し条件式); |
|
- public class DoWhile01 {
- public static void main(String[] args) {
- int i = 0; // 初期値化式
- do {
- System.out.println(i);
- i++;
- } while (i < 10);
- }
- }
□ 実行結果
$ java DoWhile01 0 1 2 3 4 5 6 7 8 9
- public class Fibonacci01 {
- public static void main(String[] args) {
- int n = Integer.parseInt(args[0]);
- int f = 1;
- int g = 1;
- int h = 0;
- System.out.print(f + " " + g + " ");
- // ここにコードを記述してください。
- System.out.println();
- }
- }
□ 実行結果
$ java Fibonacci01 1000 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987