ForNext
Only Do What Only You Can Do
028. 読み込んだ整数値までカウントアップ、0までカウントダウン (for文)
VBScript
JScript
Perl
更新日 : 2010.10.18
print "正の整数を入力してください:"; chomp($no = <>); for ($i = 0; $i <= $no; $i++) { print "$i\n"; } print "正の整数を入力してください:"; chomp($no = <>); for (0..$no) { print "$_\n"; } print "正の整数を入力してください:"; chomp($no = <>); foreach $i(0..$no) { print "$i\n"; }
L:\>perl lesson_04_028.pl 正の整数を入力してください:3 0 1 2 3 正の整数を入力してください:4 0 1 2 3 4 正の整数を入力してください:5 0 1 2 3 4 5
PHP
更新日 : 2010.11.03
<?php print "正の整数を入力してください:"; $fp = fopen("php://stdin", "r"); $no = rtrim(fgets($fp), "\n\r"); fclose($fp); for ($i = 0; $i <= $no; $i++) print "$i\n"; print "\n"; for ($i = $no; $i >= 0; $i--) print "$i\n"; ?>
L:\>php lesson_04_028.php 正の整数を入力してください:5 0 1 2 3 4 5 5 4 3 2 1 0
Python
更新日 : 2010.11.17
# coding: Shift_JIS no = int(raw_input("正の整数を入力してください:")) for i in range(0, no + 1, 1): print i print "" for i in range(no, -1, -1): print i
N:\>python lesson_04_028.py 正の整数を入力してください:5 0 1 2 3 4 5 5 4 3 2 1 0
Ruby
更新日 : 2010.11.01
print "正の整数を入力してください:" no = STDIN.gets.chomp.to_i for i in 0..no print "#{i}\n" end print "正の整数を入力してください:" no = STDIN.gets.chomp.to_i (0..no).each do |i| print "#{i}\n" end print "正の整数を入力してください:" no = STDIN.gets.chomp.to_i (no + 1).times do |i| print "#{i}\n" end print "正の整数を入力してください:" no = STDIN.gets.chomp.to_i 0.upto(no) do |i| print "#{i}\n" end print "正の整数を入力してください:" no = STDIN.gets.chomp.to_i 0.step(no, 1) do |i| print "#{i}\n" end
L:\>ruby l:\lesson_04_028.rb 正の整数を入力してください:2 0 1 2 正の整数を入力してください:3 0 1 2 3 正の整数を入力してください:4 0 1 2 3 4 正の整数を入力してください:5 0 1 2 3 4 5 正の整数を入力してください:6 0 1 2 3 4 5 6
PowerShell
Scala
F#
C
更新日 : 2010.10.08
#include <stdio.h> int main(int argc, char* argv[]) { int no, i; printf("正の整数を入力してください:"); scanf("%d", &no); for (i = 0; i <= no; i++) { printf("%d\n", i); } putchar('\n'); for (i = no; i >= 0; i--) { printf("%d\n", i); } return 0; }
R:\>lesson028\project1.exe 正の整数を入力してください:12 0 1 2 3 4 5 6 7 8 9 10 11 12 12 11 10 9 8 7 6 5 4 3 2 1 0
C++
C++Builder
VC++
C#
Java
更新日 : 2010.11.05
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 12 12 11 10 9 8 7 6 5 4 3 2 1 0
Objective-C
D
VB
VB.NET
Delphi
更新日 : 2010.09.24
program Project1; {$APPTYPE CONSOLE} uses SysUtils; var no, i: Integer; begin write('正の整数を入力してください:'); read(no); for i := 0 to no do begin writeln(format('%d', [i])); end; write(#13#10); for i := no downto 0 do begin writeln(format('%d', [i])); end; end.
S:\>lesson028\project1.exe 正の整数を入力してください:12 0 1 2 3 4 5 6 7 8 9 10 11 12 12 11 10 9 8 7 6 5 4 3 2 1 0