五次又不一定能有一方胜出,如果不限制五次就在while里面去掉count<6的判断
static void Main(string[] args)
{
game();
}
private static int result(int t)
{
Random random = new Random();
int r = (int)((1 - 4) * random.NextDouble() + 4);
switch (r)
{
case 1: if (t == 1) return 10; else if (t == 2) return 11; else return 12;
case 2: if (t == 2) return 20; else if (t == 3) return 21; else return 22;
default: if (t == 3) return 30; else if (t == 1) return 31; else return 32;
}
}
public static void game()
{
Console.WriteLine("Please Enter '1' for '剪刀'、'2' for '石头'、"
+ "'3' or others for '布':");
int a = 0, b = 0, count = 0;
while (count < 6 && a < 3 && b < 3)
{
String s = "";
while (true)
{
s = Console.ReadLine();
if (System.Text.RegularExpressions.Regex.IsMatch(s, "^[1-3]$"))//匹配输入的是不是1、2、3
break;
else
Console.WriteLine("error!");
}
int t = Convert.ToInt32(s);
int res = result(t);
if (res % 10 == 1)
{
a++;
Console.WriteLine("You: "+t + ",Computer: " + res / 10 + " Win");
}
else if (res % 10 == 2)
{
b++;
Console.WriteLine("You: "+t + ",Computer: " + res / 10 + " Lost");
}
else
Console.WriteLine("You: "+t + ",Computer: " + res / 10 + " Tie");
count++;
}
if (a == 3)
Console.WriteLine("You Win!");
else if (b == 3)
Console.WriteLine("You Lost!");
else
Console.WriteLine("Times Over!");
}
这是我写的一个小例子,就是判定谁赢谁输,你可以稍加修改,改成5局3胜的就可以了.很简单.
你可以告诉我邮箱,我发给你