class
Player
{
string name;
public
string Name
{
get {
return
name; }
set { name = value; }
}
public
int
ShowFist()
{
Console.WriteLine(
"请问,你要出什么拳? 1.剪刀 2.石头 3.布"
);
int
result = ReadInt(
1
,
3
);
string fist = IntToFist(result);
Console.WriteLine(
"玩家:{0}出了1个{1}"
, name, fist);
return
result;
}
private
string IntToFist(
int
input)
{
string result = string.Empty;
switch
(input)
{
case
1
:
result =
"剪刀"
;
break
;
case
2
:
result =
"石头"
;
break
;
case
3
:
result =
"布"
;
break
;
}
return
result;
}
private
int
ReadInt(
int
min,
int
max)
{
while
(
true
)
{
string str = Console.ReadLine();
int
result;
if
(
int
.TryParse(str, out result))
{
if
(result >= min && result <= max)
{
return
result;
}
else
{
Console.WriteLine(
"请输入1个{0}-{1}范围的数"
, min, max);
continue
;
}
}
else
{
Console.WriteLine(
"请输入整数"
);
}
}
}
}
请发表评论