猜测抛硬币的C#程序

时间:2014-02-21 04:28:59

标签: c# loops

我是编程的初学者,所以任何提示都会非常感激!

我正在尝试创建一个程序,用户猜测'抛硬币'是否会产生头部(0)尾部(1)< / strong>使用随机数生成器和循环作为'硬币'。该计划必须输出所有比赛中百分比的正确猜测。

我尝试了很多不同的方式,这是非常不对的!这令人沮丧。

提前感谢任何提示!! -C

这基本上是我最接近的:

    for (numPlays = 0; ; numPlays++)
        {
            Console.Write("\nWrite H/h to guess Heads, T/t to guess Tails, or Q/q to quit =>  ");
            userChoice = Convert.ToChar(Console.ReadLine());

            if (userChoice == 'H' || userChoice == 'h')
            {
                if (compChoice == 0)
                {
                    Console.WriteLine("\nYOU WON");
                    numWins++;
                }
                else
                    Console.WriteLine("\nYOU LOSE");

                if (userChoice == 'Q' || userChoice == 'q')
                    if (compChoice == 1)
                    {
                        Console.WriteLine("\nYOU WON");
                        numWins++;
                    }
                    else
                        Console.WriteLine("\nYOU LOSE");

                if (userChoice == 'q' || userChoice == 'Q')
                {
                    percentWin = (double)(numWins / numPlays);
                    Console.WriteLine("\nYou won {0} out of {1} game(s) or {2:P} of the games played.", numWins, numPlays, percentWin);
                }
           }

        }

        Console.ReadKey();


    }
}

3 个答案:

答案 0 :(得分:1)

我优化了你的代码,我希望这会有所帮助:

int numPlays = 0, numWins = 0;
int compChoice = 0;
char userChoice;
double percentWin;
Random rnd = new Random();
while (true)
{
     Console.Write("\nWrite H/h to guess Heads, T/t to guess Tails, or Q/q to quit =>  ");
     userChoice = Console.ReadKey().KeyChar;
     compChoice = rnd.Next(0, 2);
     if (char.ToLowerInvariant(userChoice) != 'q')
     {
           if (char.ToLowerInvariant(userChoice) == 'h' && compChoice == 0)
           {
                Console.WriteLine("\nYOU WON");
                numWins++;
           }
           else if (char.ToLowerInvariant(userChoice) == 't' && compChoice == 1)
           {
                Console.WriteLine("\nYOU WON");
                numWins++;
           }
           else
           {
                Console.WriteLine("\nYOU LOSE");
           }
           numPlays++;
     }
     else
     {
           percentWin = (double) numWins/numPlays;
           Console.WriteLine("\nYou won {0} out of {1} game(s) or {2:P} of the games played.", numWins, numPlays, percentWin);
           break;
     }

}

我认为你不需要解释,所有的代码都是不言自明的。但是当你计算百分比时你有一个明显的错误:

 percentWin = (double)(numWins / numPlays);

在这里,您正在执行integer除法,然后将结果转换为double,它是无意义,因为您已经丢失了小数点。相反,您应该将其中一个操作数强制转换为double,这样它将执行双重除法,您将获得正确的结果。

答案 1 :(得分:0)

只需调用此方法即可玩游戏!

    public static void SomeMethod()
    {
        Console.Write("\nWrite H/h to guess Heads, T/t to guess Tails, or Q/q to quit =>  ");
        char userChoice;
        int numWins = 0;
        int numPlays = 0;
        double percentWin = 0d;
        while ((userChoice = Convert.ToChar(Console.ReadLine())).ToString().ToLower() != "q")
        {
            numPlays++;

            int compChoice = new Random().Next(2);

            if (userChoice == 'H' || userChoice == 'h')
            {
                if (compChoice == 0)
                {
                    Console.WriteLine("\nYOU WON");
                    numWins++;
                }
                else
                {
                    Console.WriteLine("\nYOU LOSE");
                }
                continue;
            }
            if (userChoice == 'T' || userChoice == 't')
            {
                if (compChoice == 1)
                {
                    Console.WriteLine("\nYOU WON");
                    numWins++;
                }
                else
                {
                    Console.WriteLine("\nYOU LOSE");
                }
                continue;
            }                        
        }

        if (numPlays != 0)
        {
            percentWin = (double)numWins / numPlays;
        }
        Console.WriteLine("\nYou won {0} out of {1} game(s) or {2:P} of the games played.", numWins, numPlays, percentWin);
        Console.ReadKey();
    }

答案 2 :(得分:-2)

这应该没有任何困难,你只需要制作一个清晰的算法

我建议一个

Char playOrExit= 'y'
Int32 gameCount = 0;
Int32 Wins = 0;
While (playOrExit.ToUpper.Equals('Y'))
{
Console.Write("Press Y to continue playing and N to exit");
playOrExit = Convert.ToChar(Console.ReadLine());
//Computer choice will be math.random from 0 and 1
//now take user choice
//compare and show if he won or lost
}
if(gameCount >0)
{
Console.WriteLine("\nYou played {0} times, out of which {1} game(s) were won.",  gameCount,Wins);
}