跪求C#剪刀石头布简单代码及解释

2024-12-13 14:50:33
推荐回答(2个)
回答1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace 剪刀
{
class Program
{
static void Main(string[] args)
{

int i;
Console.WriteLine("你出啥子,输入0为剪刀。1为帕子。2为石头");
int diannao = 0, wanjia = 0;

while(true)
{
i = Console.Read(); Console.Read(); Console.Read();//read后面自动加\r\n所以要加两个来吸收
int c = panduan(i);
if (c== 0)
{
Console.WriteLine("电脑赢了");
diannao++;
}
else if (c== 1)
{
Console.WriteLine("你赢了");
wanjia++;
}
Console.WriteLine("你赢了"+wanjia+"次"+"电脑赢了"+diannao+"次");
Console.WriteLine();
if (diannao == 3|| wanjia == 3)
{
Console.WriteLine("game over");
return;
}

}

}
static int panduan(int i)
{
int c;
string [] s=new string[3];
s[0] = "剪刀";
s[1] = "帕子";
s[2] = "石头";

if (i < 48 || i > 50)
{
Console.WriteLine("乱输嘛,算你输"+i);
return 0;
}
Console.WriteLine("你的输入时" + s[i-48]);
Random a = new Random();
c = a.Next(48,50);
Console.WriteLine("电脑出的是"+s[c-48]);

if(i==c)
{
return 2 ;
}
if (i == 48 && c == 49 || i == 49 && c == 50 || i == 50 && c == 48)
return 1;
else
return 0;
}
}
}

回答2:

以前写的一个,你试试: Random obj = new Random();
int Num = 0;
int WinNum = 0;
int LostNum = 0;
int AndNum = 0;
//筹码
int Psn = 100;
int Cmr = 100;
//连赢次数
int PsnTime = 0;
int CmrTime = 0;
Console.WriteLine("剩余筹码:玩家:{0} 电脑:{1}", Psn, Cmr);
Console.Write("你的选择:");
string str = Convert.ToString(Console.ReadLine());
while (str!="exit")
{ int Computer = obj.Next(1, 4);
if (str == "剪刀")
{
switch (Computer)
{
case 1:
Console.WriteLine("玩家:{0} VS 电脑:剪刀 平局!!!", str);
AndNum++;
Num++;
break;
case 2:
Console.WriteLine("玩家:{0} VS 电脑:石头 很遗憾,你输啦!!!", str);
CmrTime++;
PsnTime = 0;
Psn--;
Cmr++;
LostNum++;
Num++;
break;
case 3:
Console.WriteLine("玩家:{0} VS 电脑:布 恭喜你,你赢啦!!!", str);
PsnTime++;
CmrTime = 0; Psn++;
Cmr--;
WinNum++;
Num++;
break;
default:
break;
}
}
if (str == "石头")
{
switch (Computer)
{
case 1:
Console.WriteLine("玩家:{0} VS 电脑:剪刀 恭喜你,你赢啦!!!", str);
PsnTime++;
CmrTime = 0; Psn++;
Cmr--; WinNum++;
Num++;
break;
case 2:
Console.WriteLine("玩家:{0} VS 电脑:石头 平局!!!", str);
AndNum++;
Num++;
break;
case 3:
Console.WriteLine("玩家:{0} VS 电脑:布 很遗憾,你输啦!!!", str);
CmrTime++;
PsnTime = 0; Psn--;
Cmr++; LostNum++;
Num++;
break;
default:
break;
}
}