猜拳(三局两胜)
请输入您的手势:石头
用户手势:石头 电脑手势:剪刀
用户胜:1 电脑胜:0
请输入您的手势:石头
用户手势:石头 电脑手势:石头
用户胜:1 电脑胜:0
请输入您的手势:石头
用户手势:石头 电脑手势:包袱
用户胜:1 电脑胜:1
请输入您的手势:石头
用户手势:石头 电脑手势:剪刀
用户胜:2 电脑胜:1
用户胜利!!!
做了一个三局两胜的猜拳游戏,顺便帮自己回忆下,也为了更加熟练的运用C#基本代码
int user = ;
int computer = ;
for (; ; )
{ Console.WriteLine("请输入手势:剪刀、石头、布");
string x = Console.ReadLine();
Random ran = new Random();
int i = ran.Next(, ); int a;
string b;
//手势转换
if (x == "剪刀")
{
a = ;
}
else if (x == "石头")
{
a = ;
}
else
{
a = ;
}
//随机数转换
if (i == )
{
b = "剪刀";
}
else if (i == )
{
b = "石头";
}
else
{
b = "布";
} //手势判断
if ((a == && i == ) || (a == && i == ) || (a == && i == ))
{
Console.WriteLine("人类手势:" + x + "\t电脑手势:" + "\0" + b);
Console.WriteLine("平局!");
Console.WriteLine();
}
else if ((a == && i == ) || (a == && i == ) || (a == && i == ))
{
Console.WriteLine("人类手势:" + x + "\t电脑手势:" + "\0" + b);
Console.WriteLine("人类胜出!");
Console.WriteLine();
user++;
}
else
{
Console.WriteLine("人类手势:" + x + "\t电脑手势:" + "\0" + b);
Console.WriteLine("电脑胜出!");
Console.WriteLine();
computer++;
} Console.WriteLine(); if (user == )
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("人类胜利" + user + "局" + " " + "电脑胜利" + computer + "局");
Console.WriteLine();
Console.WriteLine(" 人类最终胜出!!!");
break;
}
else if (computer == )
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("人类胜利" + user + "局" + " " + "电脑胜利" + computer + "局");
Console.WriteLine();
Console.WriteLine(" 电脑最终胜出!!!");
break;
} Console.WriteLine("-------------下一局-------------"); } Console.ReadLine();
写起来挺有意思的,感觉越来越好玩儿了