C#计数器游戏机应用程序

时间:2015-10-22 23:44:27

标签: c# console

var choices = new Dictionary<int, string> { { 1, "Rock" }, { 2, "Paper" }, { 3, "Scissors" } };
int input;
var randomly = new Dictionary<int, string> { { 1, "Rock" }, { 2, "Paper" }, { 3, "Scissors" } };

Random random = new Random();
int RandomNumber = random.Next(1, 4);

do
{
    //Counter
    double Wins = 0.0;
    int Losses = 0;
    int Ties = 0;

    Console.WriteLine("-- Weapons Menu --");
    Console.WriteLine("-------------------");
    Console.WriteLine("1] Rock");
    Console.WriteLine("2] Paper");
    Console.WriteLine("3] Scissors");
    Console.WriteLine("Choose Your Weapon [1, 2 or 3]:");
    Console.WriteLine();
    input = int.Parse(Console.ReadLine());
    if (input == RandomNumber)
        Console.WriteLine("You Tied The Computer");
    Ties++;
    //Winning 
    ++Wins;
    if (input == 1 && RandomNumber == 3)

    Console.WriteLine("You Beat The Computer :)");


    if (input == 2 && RandomNumber == 1)

    Console.WriteLine("You Beat The Computer :)");


    if (input == 3 && RandomNumber == 2)

    Console.WriteLine("You Beat The Computer :)");

    //Lossing 
    ++Losses;
    if (input == 1 && RandomNumber == 2)
        Console.WriteLine("Sorry You Lost To The Computer :(");

    if (input == 2 && RandomNumber == 3)
        Console.WriteLine("You Beat The Computer :)");

    if (input == 3 && RandomNumber == 1)
        Console.WriteLine("You Beat The Computer :)");

    Console.WriteLine(string.Format("Player Chose {0} : Computer Chose {1}", choices[input], randomly[RandomNumber]));
    Console.WriteLine();
    Console.WriteLine("<Press Any Key To Continue...>");
    Console.WriteLine();

    Console.WriteLine("Player Wins      Computer Wins");
    Console.WriteLine("----------       --------------");
    Console.WriteLine(" {0}                {1}", Wins, Losses);
    Console.WriteLine();

    Console.ReadKey();
}
while (input != 4) ;
Console.WriteLine("Thanks for playing!");

虽然我无法在每场比赛结束后正确获得胜负记录,但代码运行良好。

2 个答案:

答案 0 :(得分:1)

此代码是您的问题(以及获胜/损失的进一步下载)

        if (input == RandomNumber)
            Console.WriteLine("You Tied The Computer");
        Ties++;

需要写成:

        if (input == RandomNumber){
            Console.WriteLine("You Tied The Computer");
            Ties++;
        }

解释:目前你输出Tied只有在input == randomnumber没有问题的情况下才会输出,但是无论是否打平,你都会将领带增加1。与获胜相同,同样也有损失。

IF语句只会执行下一行。如果你有超过1行,你需要执行If语句的结果,那么你需要在{}

中包含那些多行

答案 1 :(得分:0)

除了Nikerym的回答:对于输赢的逻辑,将三个&#34; if&#34;陈述成单一的&#34; if&#34;声明,并将增量语句移动到&#34;然后&#34;块。

if ((input == 1 && RandomNumber == 3) || 
    (input == 2 && RandomNumber == 1) ||
    (input == 3 && RandomNumber == 2)) {

    Console.WriteLine("You Beat The Computer :)");
    Wins++;
}

同样的损失想法。编写程序的方式,总是会增加胜利和损失。 Wins也应该是int,而不是double。