c#猜拳程序的源码
以下是我写的代码。希望对你有帮助。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RockPaperScissors
{
class Program
{
static void Main(string[] args)
{
//随机数生成器,用于系统随机出拳
Random random = new Random();
int myShow;//系统出的拳,1、石头,2、剪刀,3、布
int yourShow;//玩家出的拳,1、石头,2、剪刀,3、布
int myScore = 祥轮0;//系统成绩
int yourScore = 0;//玩家成绩
int drawCount = 0;//平局次数
string result;//保前乎存每局的结果
Console.WriteLine("Rock, paper, scissors\n"
+ "---------------------\n"
+ "Rock beats scissors,\n"
+ "Scissors beats paper,\n"
+ "Paper beats rock.\n"
+ "Enter 1-rock 2-scissors 3-paper");
do
{
myShow = random.Next(3) + 1;//生成一个1~3的随机数
yourShow = int.Parse(Console.ReadLine()) % 3;//用户输入的数
if (yourShow == 0)
{
yourShow = 3;
}
//平局
if (myShow == yourShow)
{
result = "Draw";
drawCount++;
}
//用户胜,判断条件也可写成yourShow - myShow == -1 || yourShow-myShow == 2
else if ((yourShow - myShow + 3) % 3 == 2)
{
result = "You won";
yourScore++;
}
//系统胜
else
{
result = "I won";
myScore++;
}
//显示成绩
Console.WriteLine("Me: {0}, You: {1} -- {2}.",
myShow == 1 ? "rock" : myShow == 2 ? "scissors" : "paper",
yourShow == 1 ? "rock" : yourShow == 2 ? "scissors" : "谨悔信paper",
result);
Console.WriteLine("You scored {0}, I scored {1} with {2} draws.",
yourScore, myScore, drawCount);
Console.WriteLine(yourScore > myScore ? "You are winning." : yourScore < myScore ? "I am winning." : "We are draw");
//提示用户选择是否继续
Console.Write("Another game? (y/n) ");
if (Console.ReadLine().ToLower() == "n")
{
break;
}
Console.WriteLine("Enter 1-rock 2-scissors 3-paper");
} while (true);
}
}
}
广告 您可能关注的内容 |