using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请出拳,1----剪刀,2----石头,3----布");
string result = Console.ReadLine();
int number = GetConvert(result);
Random r = new Random();
int numberTwo = r.Next(1, 4);
result = GetReferee(number, numberTwo);
Console.WriteLine("你出的是:{0}", UserShuRu(number));
Console.WriteLine("电脑出的是:{0}", UserShuRu(numberTwo));
Console.WriteLine("结果:{0}", result);
Console.ReadKey();
}
/// <summary>
/// 裁判
/// </summary>
/// <param name="numberOne"></param>
/// <param name="numberTwo"></param>
/// <returns></returns>
public static string GetReferee(int numberOne, int numberTwo)
{
string referee = " ";
if (numberOne == numberTwo)
{
referee = "平手";
}
else if (numberOne > numberTwo)
{
referee = "赢";
}
else
{
referee = "输";
}
return referee;
}
/// <summary>
/// 玩游戏
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public static string UserShuRu(int number)
{
string result = " ";
switch (number)
{
case 1: result = "剪刀";
break;
case 2: result = "石头";
break;
default: result = "布";
break;
}
return result;
}
/// <summary>
/// 限定用户的输入,用户只要输入的不是1或者2或者3,就提示用户重新输入
/// </summary>
/// <param name="str">传入用户输入的字符串</param>
/// <returns>返回一个数字</returns>
public static int GetConvert(string str)
{
int number = 0;
while (true)
{
try
{
number = Convert.ToInt32(str);
if (number >= 1 && number <= 3)
{
return number;
}
else
{
Console.WriteLine("你的输入有误,请输入1----剪刀,2----石头,3----布");
str = Console.ReadLine();
}
}
catch
{
Console.WriteLine("你输入的不是数字,请输入1----剪刀,2----石头,3----布");
str = Console.ReadLine();
}
}
}
}
}
效果图
---------------------- 、期待与您交流! ----------------------