ForNext
Only Do What Only You Can Do
062. 選ばれた動物の鳴き声を表示 (列挙体)
VBScript
JScript
Perl
PHP
Python
Ruby
PowerShell
Scala
F#
C
更新日 : 2010.10.08
#include <stdio.h> enum animal { Dog , Cat , Monkey , Invalid }; void dog(void) { puts("ワンワン!!"); } void cat(void) { puts("ニャ〜オ!!"); } void monkey(void) { puts("キッキッ!!"); } enum animal select(void) { int tmp; do { printf("0…犬 1…猫 2…猿 3…終了"); scanf("%d", &tmp); } while (tmp < Dog || tmp > Invalid); return (enum animal)tmp; } int main(int argc, char* argv[]) { enum animal selected; do { switch (selected = select()) { case Dog: dog(); break; case Cat: cat(); break; case Monkey: monkey(); break; } } while (selected < Invalid); return 0; }
R:\>lesson062\Project1.exe 0…犬 1…猫 2…猿 3…終了0 ワンワン!! 0…犬 1…猫 2…猿 3…終了1 ニャ〜オ!! 0…犬 1…猫 2…猿 3…終了2 キッキッ!! 0…犬 1…猫 2…猿 3…終了9 0…犬 1…猫 2…猿 3…終了3
C++
更新日 : 2010.10.13
#include <stdio.h> enum animal { Dog , Cat , Monkey , Invalid }; void dog(void) { puts("ワンワン!!"); } void cat(void) { puts("ニャ〜オ!!"); } void monkey(void) { puts("キッキッ!!"); } animal select(void) { int tmp; do { printf("0…犬 1…猫 2…猿 3…終了"); scanf("%d", &tmp); } while (tmp < Dog || tmp > Invalid); return animal(tmp); } int main(int argc, char* argv[]) { animal selected; do { switch (selected = select()) { case Dog: dog(); break; case Cat: cat(); break; case Monkey: monkey(); break; } } while (selected < Invalid); return 0; }
T:\>lesson062\Project1.exe 0…犬 1…猫 2…猿 3…終了0 ワンワン!! 0…犬 1…猫 2…猿 3…終了1 ニャ〜オ!! 0…犬 1…猫 2…猿 3…終了2 キッキッ!! 0…犬 1…猫 2…猿 3…終了9 0…犬 1…猫 2…猿 3…終了3
C++Builder
VC++
C#
Java
更新日 : 2010.11.05
import java.util.Scanner; class Lesson062 { enum animal { Dog(0), Cat(1), Monkey(2), Invalid(3); private int value; animal(int value) { this.value = value; } // enum定数から整数へ変換 int getIntValue() { return this.value; } // 整数からenum定数へ変換 static animal valueOf(final int value) { for (animal d : values()) { if (d.getIntValue() == value) { return d; } } return null; } } public static void main(String[] args) { animal selected; do { switch (selected = select()) { case Dog: dog(); break; case Cat: cat(); break; case Monkey: monkey(); break; } } while (selected.getIntValue() < animal.Invalid.getIntValue()); } static animal select() { Scanner stdIn = new Scanner(System.in); int tmp; do { System.out.printf("0…犬 1…猫 2…猿 3…終了"); tmp = stdIn.nextInt(); } while (tmp < animal.Dog.getIntValue() || tmp > animal.Invalid.getIntValue()); return animal.valueOf(tmp); } static void dog() { System.out.println("ワンワン!!"); } static void cat() { System.out.println("ニャ〜オ!!"); } static void monkey() { System.out.println("キッキッ!!"); } }
L:\>java Lesson062 0…犬 1…猫 2…猿 3…終了0 ワンワン!! 0…犬 1…猫 2…猿 3…終了1 ニャ〜オ!! 0…犬 1…猫 2…猿 3…終了2 キッキッ!! 0…犬 1…猫 2…猿 3…終了9 0…犬 1…猫 2…猿 3…終了3
Objective-C
D
VB
VB.NET
Delphi
更新日 : 2010.09.24
program Project1; {$APPTYPE CONSOLE} uses SysUtils; type TAnimal = ( Dog , Cat , Monkey , Invalid ); procedure dog(); begin writeln('ワンワン!!'); end; procedure cat(); begin writeln('ニャ〜オ!!'); end; procedure monkey(); begin writeln('キッキッ!!'); end; function select():TAnimal; var tmp: Integer; begin repeat write('0…犬 1…猫 2…猿 3…終了'); read(tmp); until (Integer(Dog) <= tmp) and (tmp <= Integer(Invalid)); result := TAnimal(tmp); end; procedure main(); var selected: TAnimal; begin repeat selected := select; case selected of Dog: dog; Cat: cat; Monkey: monkey; end; until (selected >= Invalid); end; begin main; end.
S:\>lesson062\Project1.exe 0…犬 1…猫 2…猿 3…終了0 ワンワン!! 0…犬 1…猫 2…猿 3…終了1 ニャ〜オ!! 0…犬 1…猫 2…猿 3…終了2 キッキッ!! 0…犬 1…猫 2…猿 3…終了9 0…犬 1…猫 2…猿 3…終了3