ForNext
Only Do What Only You Can Do
027. 読み込んだ整数値を0までカウントダウン (while文)
VBScript
JScript
Perl
更新日 : 2010.10.18
print "正の整数を入力してください:"; chomp($no = <>); while ($no >= 0) { printf("%d\n", $no--); } print "正の整数を入力してください:"; chomp($no = <>); printf("%d\n", $no--) while ($no >= 0); print "正の整数を入力してください:"; chomp($no = <>); until ($no < 0) { printf("%d\n", $no--); } print "正の整数を入力してください:"; chomp($no = <>); printf("%d\n", $no--) until ($no < 0);
L:\>perl lesson_04_027.pl 正の整数を入力してください:7 7 6 5 4 3 2 1 0 正の整数を入力してください:6 6 5 4 3 2 1 0 正の整数を入力してください:5 5 4 3 2 1 0 正の整数を入力してください:4 4 3 2 1 0
PHP
更新日 : 2010.11.03
<?php $fp = fopen("php://stdin", "r"); print "正の整数を入力してください:"; $no = rtrim(fgets($fp), "\n\r"); while ($no >= 0) { printf("%d\n", $no--); } fclose($fp); ?>
L:\>php lesson_04_027.php 正の整数を入力してください:5 5 4 3 2 1 0
Python
更新日 : 2010.11.17
# coding: Shift_JIS no = int(raw_input("正の整数を入力してください:")) while no >= 0: print "%d" % no no -= 1
N:\>python lesson_04_027.py 正の整数を入力してください:5 5 4 3 2 1 0
Ruby
更新日 : 2010.11.01
print "正の整数を入力してください:" no = STDIN.gets.chomp.to_i while (no >= 0) printf("%d\n", no) no -= 1 end print "正の整数を入力してください:" no = STDIN.gets.chomp.to_i until (no < 0) printf("%d\n", no) no -= 1 end
L:\>ruby l:\lesson_04_027.rb 正の整数を入力してください:11 11 10 9 8 7 6 5 4 3 2 1 0 正の整数を入力してください:0 0 L:\>ruby l:\lesson_04_027.rb 正の整数を入力してください:-5 正の整数を入力してください:0 0
PowerShell
Scala
F#
C
更新日 : 2010.10.08
#include <stdio.h> int main(int argc, char* argv[]) { int no; printf("正の整数を入力してください:"); scanf("%d", &no); while (no >= 0) { printf("%d\n", no--); } return 0; }
R:\>lesson027\project1.exe 正の整数を入力してください:11 11 10 9 8 7 6 5 4 3 2 1 0 R:\>lesson027\project1.exe 正の整数を入力してください:0 0 R:\>lesson027\project1.exe 正の整数を入力してください:-5
C++
C++Builder
VC++
C#
Java
更新日 : 2010.11.05
import java.util.Scanner; class Lesson027 { public static void main(String[] args) { System.out.print("正の整数を入力してください:"); Scanner stdIn = new Scanner(System.in); int no = stdIn.nextInt(); while (no >= 0) { System.out.printf("%d\n", no--); } } }
L:\>java Lesson027 正の整数を入力してください:11 11 10 9 8 7 6 5 4 3 2 1 0 L:\>java Lesson027 正の整数を入力してください:0 0 L:\>java Lesson027 正の整数を入力してください:-5
Objective-C
D
VB
VB.NET
Delphi
更新日 : 2010.09.24
program Project1; {$APPTYPE CONSOLE} uses SysUtils; var no: Integer; begin write('正の整数を入力してください:'); read(no); while (no >= 0) do begin writeln(format('%d', [no])); dec(no); end; end.
S:\>lesson027\project1.exe 正の整数を入力してください:11 11 10 9 8 7 6 5 4 3 2 1 0 S:\>lesson027\project1.exe 正の整数を入力してください:0 0 S:\>lesson027\project1.exe 正の整数を入力してください:-5