読み込んだ整数値までカウントアップ、0までカウントダウン (for文)
明解C言語 入門編 > 4. プログラムの流れの繰り返し >
読み込んだ整数値までカウントアップ、0までカウントダウン (for文)
Java
import java.util.Scanner; class Lesson028 { public static void main(String[] args) { System.out.print("正の整数を入力してください:"); Scanner stdIn = new Scanner(System.in); int no = stdIn.nextInt(); for (int i = 0; i <= no; i++) { System.out.printf("%d\n", i); } System.out.print('\n'); for (int i = no; i >= 0; i--) { System.out.printf("%d\n", i); } } }
実行結果
L:\>java Lesson028
正の整数を入力してください:12
0
1
2
3
4
5
6
7
8
9
10
11
1212
11
10
9
8
7
6
5
4
3
2
1
0