【问题标题】:C# - Keeping total number of correct guesses inside a variableC# - 在变量中保留正确猜测的总数
【发布时间】:2020-12-27 18:39:48
【问题描述】:

编程初学者并被分配了一个正面或反面硬币翻转项目。

我想出了如何让它随机生成请求的翻转次数并打印出答案。但是,我应该将用户输入出现在correctCount 变量中的次数加起来,但我找不到任何方法。我试过在这里和不同的网站上搜索,而不是在整个谷歌上搜索。我假设我需要获取他们的字符串输入并以某种方式对其进行转换,例如if result == string,然后基本上是correctCount + 1,但无法弄清楚如何实现这一点,因为您无法使用int 和@987654325 做到这一点@。任何信息或提示都会非常有帮助 - 谢谢!

    class Coin
    {

        static void Main(string[] args)

        // UserInput
        {
            Console.Write("Guess which will have more: heads or tails?");
            string headsOrTailsGuess = Console.ReadLine() + "\n";
  

            Console.Write("\n" + "How many times shall we flip the coin? ");
            int numberOfFlips = int.Parse(Console.ReadLine() + "\n");


            // Declare variables
            int correctCount = 0;
            int heads = 0;
            int tails = 1;

            Random rand = new Random();

            for (int i = 0; i < numberOfFlips; i++)
            {
                int result = rand.Next(0, 2);

                if (result == 0)
                {
                    Console.WriteLine("Heads!");
                } 
                else 
                {
                    Console.WriteLine("Tails!");
                }
 
            }
            Console.WriteLine("Your guess, " + headsOrTailsGuess + "came up " + correctCount + " time(s).");```

【问题讨论】:

  • 您需要先将输入字符串headsOrTailsGuess 转换为计算机可以使用的字符串。最好是 int 或 bool,然后你可以在 if 语句中进行计数。
  • 严格来说指令要求用户输入headstails。然后代码继续评估 Heads 或 Tails。此外,int.Parse 不适用于用户输入的非数字。使用TryParse
  • 我从未见过Console.ReadLine() + "\n",除非它是一个超级秘密魔法,否则+ "\n" 部分毫无用处。

标签: c# coin-flipping


【解决方案1】:

正如 PMF 所提到的,您不仅要接收用户的选择,还要对其进行分析。 尽管您可能想为用户输入添加一些验证,但在这里进行一个简单的比较就足够了。

static void Main(string[] args)

    // UserInput
    {
        int choice; //variable for parsed user choice
        Console.Write("Guess which will have more: heads or tails?");
        string headsOrTailsGuess = Console.ReadLine() + "\n";
        if(headsOrTailsGuess.ToLower().Trim() == "heads"){ //here you look if its heads
            choice = 0;
        }
        else{ //we can write additional if here to avoid other options from counting as tails
            choice = 1;
        }

        Console.Write("\n" + "How many times shall we flip the coin? ");
        int numberOfFlips = int.Parse(Console.ReadLine() + "\n");


        // Declare variables
        int correctCount = 0;
        int heads = 0;
        int tails = 1;

        Random rand = new Random();

        for (int i = 0; i < numberOfFlips; i++)
        {
            int result = rand.Next(0, 2);

            if (result == 0)
            {
                Console.WriteLine("Heads!");
            } 
            else 
            {
                Console.WriteLine("Tails!");
            }
            if(result == choice){ //comparing result to parsed choice and incrementing the counter
                correctCount++; 
            }

        }
        Console.WriteLine("Your guess, " + headsOrTailsGuess + "came up " + correctCount + " time(s).");

}

【讨论】:

    【解决方案2】:

    这是使用 Linq 的另一种方法。

    using System.Collections.Generic;
    using System.Linq;
    
    public class Program
    {
        public static void Main()
        {
             // UserInput
            {
                Console.Write("Guess which will have more: heads or tails?");
                string headsOrTailsGuess = Console.ReadLine();
      
                Console.Write("\n" + "How many times shall we flip the coin? ");
                int numberOfFlips = int.Parse(Console.ReadLine() + "\n");
    
                // Declare variables
                List<string> resultList  = new List<string>();
                Random rand = new Random();
          
                for (int i = 0; i < numberOfFlips; i++)
                {
                    int result = rand.Next(0, 2);
                    resultList.Add(result == 1? "Heads!" : "Tails!");
                }
                var guessCount  = resultList.Where(a=>a.ToUpper().Contains(headsOrTailsGuess.ToUpper())).Count();
                resultList.ForEach( a=> Console.WriteLine(a));
                Console.WriteLine("Your guess, " + headsOrTailsGuess + " came up " + guessCount + " time(s).");
                Console.WriteLine(headsOrTailsGuess);
            }
        }
    }
    

    【讨论】:

      【解决方案3】:
      using System;
      
      Console.WriteLine("Guess which will have more: heads or tails?");
      
      var headsChosen = false;
      
      var input = string.Empty;
      do
      {
          input = Console.ReadLine();
      
          headsChosen = input.ToLower() == "heads";
      
      } while (input.ToLower() != "heads" && input.ToLower() != "tails");
      
      
      Console.WriteLine("How many times shall we flip the coin?");
      
      var flips = 0;
      
      do
      {
      } while (!int.TryParse(Console.ReadLine(), out flips));
      
      var rnd = new Random();
      
      var heads = 0;
      var tails = 0;
      
      for (int i = 0; i < flips; i++)
      {
          if (rnd.Next(0, 2) == 0)
          {
              Console.WriteLine("Heads!");
              heads++;
          }
          else
          {
              Console.WriteLine("Tails!");
              tails++;
          }
      }
      
      var userGuessed = (headsChosen && heads > tails) || (!headsChosen && tails > heads);
      
      Console.WriteLine($"You {(userGuessed ? "guess" : "loose")}!");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-28
        • 2011-08-26
        • 1970-01-01
        • 2019-08-25
        • 1970-01-01
        相关资源
        最近更新 更多