随机数生成器失败

时间:2016-04-30 20:31:09

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

如果选择了错误的号码,如何让程序从头开始循环?我不确定我做错了什么。我尝试了ifdo while s,whileif else s:

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;
            }
        }
    }
}

3 个答案:

答案 0 :(得分: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);                
        }

    }

答案 1 :(得分:0)

您已走上正轨,但只有当用户的值匹配时,您才需要将Console.ReadLine置于while循环内并将break置于循环之外。

像这样的伪代码:

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

答案 2 :(得分:0)

我会使用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)
}