開発環境
- Microsoft Windows 8 Pro 64bit 日本語 (OS)
- Microsoft Visual Studio Express 2012 for Windows Desktop (IDE)
- プログラミング言語: C#
『初めてのC# 第2版』(Jesse Liberty+Brian MacDonald著、日向俊二訳、オライリー・ジャパン、2006年、ISBN978-487311-294-7)の 第5章(実行制御)5.5(練習問題)練習5-2を解いてみる。
その他参考書籍
練習5-2.
コード
using System;
using System.Text.RegularExpressions;
class Tester
{
enum Conditions
{
big, mul, even, odd, hatena,
}
public void Run()
{
Regex reg = new Regex(@"^\s*$");
while (true)
{
try
{
Console.Write("数値を入力: ");
string str = Console.ReadLine();
if (reg.IsMatch(str)) break;
int n = Convert.ToInt16(str);
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("?");
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
while (true)
{
try
{
Console.Write("数値を入力: ");
string str = Console.ReadLine();
if (reg.IsMatch(str))
{
break;
}
int n = Convert.ToInt16(str);
Conditions c = n >= 100 ? Conditions.big :
n % 10 == 0 ? Conditions.mul :
n % 2 == 0 ? Conditions.even :
n % 2 == 1 ? Conditions.odd :
Conditions.hatena;
switch (c)
{
case Conditions.big:
Console.WriteLine("大きすぎる値");
break;
case Conditions.mul:
Console.WriteLine("10の倍数");
break;
case Conditions.even:
Console.WriteLine("偶数");
break;
case Conditions.odd:
Console.WriteLine("奇数");
break;
case Conditions.hatena:
Console.WriteLine("?");
break;
default:
break;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
static void Main()
{
Tester t = new Tester();
t.Run();
}
}
入出力結果(Console Window)
数値を入力: 100 大きすぎる値 数値を入力: 80 10の倍数 数値を入力: 88 偶数 数値を入力: 87 奇数 数値を入力: c# 入力文字列の形式が正しくありません。 数値を入力: 数値を入力: 100 大きすぎる値 数値を入力: 80 10の倍数 数値を入力: 88 偶数 数値を入力: 87 奇数 数値を入力: c# 入力文字列の形式が正しくありません。 数値を入力: 続行するには何かキーを押してください . . .
ちなみにJavaScriptの場合。
コード(BBEdit)
var result = "if/else if/else文\n";
var n = parseInt($('#t0').val());
if(n >= 100){
result += "大きすぎる値です\n";
} else if(n % 10 === 0){
result += "10の倍数です。\n";
} else if(n % 2 === 0){
result += "偶数です。\n";
} else if (n % 2 === 1){
result += "奇数です\n";
} else {
result += "?\n";
}
result += "switch文\n";
var c = n >= 100 ? "big" :
n % 10 === 0 ? "mul" :
n % 2 === 0 ? "even" :
n % 2 === 1 ? "odd" : "?";
switch(c){
case "big": result += "大きすぎる値\n"; break;
case "mul": result += "10の倍数\n"; break;
case "even": result += "偶数\n"; break;
case "odd": result += "奇数\n"; break;
case "?": result += "?"; break;
default: result += "??";
}
$('#pre0').text(result);
pythonの場合。
sample.py
コード(BBEdit)
#!/usr/bin/env python3.3
# -*- coding: utf-8 -*-
import re
while True:
s = input("数値を入力: ")
if re.match(r"^\s*$", s): break
try:
n = int(s)
if n >= 100: print("大きすぎる値")
elif n % 10 == 0: print("10の倍数")
elif n % 2 == 0: print("偶数")
elif n % 2 == 1: print("奇数")
else: print("?")
except Exception as err:
print(err)
入出力結果(Terminal)
$ ./sample.py 数値を入力: python invalid literal for int() with base 10: 'python' 数値を入力: 100 大きすぎる値 数値を入力: 80 10の倍数 数値を入力: 88 偶数 数値を入力: 87 奇数 数値を入力: $
0 コメント:
コメントを投稿