【问题标题】:Random Number generator fail随机数生成器失败
【发布时间】:2016-08-25 21:20:30
【问题描述】:

如果选择了不正确的数字,如何让程序从头开始循环?我不确定我做错了什么。我试过ifs、do whiles、whiles 和if elses:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArrayProblms
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Guess a number between 1 and 10: ");
            RandomNumberGenerator();
            Console.ReadLine();
        }

        public static void RandomNumberGenerator()
        {
            Random rand = new Random();
            int userValue = int.Parse(Console.ReadLine());
            int randValue = rand.Next(1, 11);
            int attempts = 0;

            if (userValue == randValue)
            {
                Console.WriteLine("You have guessed correctly!");
            }
            while (userValue != randValue)
            {
                Console.WriteLine("You have guessed incorrectly");
                attempts++;
                Console.WriteLine("You have made {0} incorrect guesses", attempts);
                break;
            }
        }
    }
}

【问题讨论】:

  • 程序结束的条件是什么,反复循环的条件是什么?

标签: c# linq visual-studio functional-programming


【解决方案1】:

您应该将int userValue = int.Parse(Console.ReadLine()); 放入循环中,并在每次迭代时检查输入。 break 必须仅当 userValue == randValue:

    public static void RandomNumberGenerator()
    {
        Random rand = new Random();

        int randValue = rand.Next(1, 11);
        int attempts = 0;


        while (true)
        {
            int userValue = int.Parse(Console.ReadLine()); // input inside the loop
            if (userValue == randValue) // checking inside the loop
            {
                Console.WriteLine("You have guessed correctly!");
                break;
            }

            Console.WriteLine("You have guessed incorrectly");
            attempts++;
            Console.WriteLine("You have made {0} incorrect guesses",            attempts);                
        }

    }

【讨论】:

    【解决方案2】:

    我会使用do...while 继续要求用户输入新号码,直到他猜对为止。

    下面的例子:

    public static void RandomNumberGenerator()
    {
        Random rand = new Random();
    
        int randValue = rand.Next(1, 11);
        int attempts = 0;
    
        // do...while cycle to ask user to enter new value each time the used has been wrong
        do
        {
            // read user input
            int userValue = int.Parse(Console.ReadLine());
    
            // if user guessed correct
            if (userValue == randValue)
            {
                Console.WriteLine("You have guessed correctly!");
                // go away from do...while loop
                // it will stop asking user and will exit from the method
                break;
            }
    
            // if user has been wrong
            Console.WriteLine("You have guessed incorrectly");
            // increment attempts count
            attempts++;
            Console.WriteLine("You have made {0} incorrect guesses", attempts);
        }
        // and repeat until user guessed correctly
        while(userValue != randValue)
    }
    

    【讨论】:

    • Try this: 没有解释任何内容。我们是否必须逐行比较代码才能看到您修复了什么?
    • 我放了一个 cmets 来描述示例代码的作用。我希望它会有所帮助
    【解决方案3】:

    您在正确的轨道上,但您需要将 Console.ReadLine 放入 while 循环中,并且仅当用户的值匹配时,break 才会退出循环。

    类似这样的伪代码:

    Generate random number
    while (true) {
        Get value from user
        If it matches, break
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-03
      • 1970-01-01
      • 1970-01-01
      • 2015-03-14
      • 1970-01-01
      • 1970-01-01
      • 2017-04-05
      • 2011-01-24
      相关资源
      最近更新 更多