開発環境
- Microsoft Windows 8 Pro 64bit 日本語 (OS)
- Microsoft Visual Studio Express 2012 for Windows Desktop (IDE)
- プログラミング言語: C#
『初めてのC# 第2版』(Jesse Liberty+Brian MacDonald著、日向俊二訳、オライリー・ジャパン、2006年、ISBN978-487312-194-7)の 第5章(実行制御)5.5(練習問題)練習5-2.を解いてみる。
その他参考書籍
練習5-2.
コード
using System;
class Tester
{
enum Conditions
{
big, multiply, even, odd, other,
}
public void Run()
{
while (true)
{
string s = Console.ReadLine();
if (s == "")
{
break;
}
int n = Convert.ToInt16(s);
if (n >= 100)
{
Console.WriteLine("大きすぎる値");
}
else if (n % 10 == 0)
{
Console.WriteLine("10の倍数");
}
else if (n % 2 == 0)
{
Console.WriteLine("偶数");
}
else if (n % 2 == 1)
{
Console.WriteLine("奇数");
}
else
{
Console.WriteLine("?");
}
Conditions c = n >= 100 ? Conditions.big :
n % 10 == 0 ? Conditions.multiply :
n % 2 == 0 ? Conditions.even :
n % 2 == 1 ? Conditions.odd :
Conditions.other;
switch (c)
{
case Conditions.big:
Console.WriteLine("大きすぎる値");
break;
case Conditions.multiply:
Console.WriteLine("10の倍数");
break;
case Conditions.even:
Console.WriteLine("偶数");
break;
case Conditions.odd:
Console.WriteLine("奇数");
break;
case Conditions.other:
Console.WriteLine("?");
break;
default:
break;
}
}
}
static void Main()
{
Tester t = new Tester();
t.Run();
}
}
入出力結果(Console Window)
100 大きすぎる値 大きすぎる値 99 奇数 奇数 98 偶数 偶数 90 10の倍数 10の倍数 50 10の倍数 10の倍数 59 奇数 奇数 58 偶数 偶数 10 10の倍数 10の倍数 9 奇数 奇数 8 偶数 偶数 7 奇数 奇数 6 偶数 偶数 5 奇数 奇数 続行するには何かキーを押してください . . .
0 コメント:
コメントを投稿