簡體   English   中英

隨機數生成器失敗

[英]Random Number generator fail

如果選擇了錯誤的編號,如何使程序從頭開始循環播放? 我不確定自己在做什么錯。 我想if S, do while S, while S,以及if 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;
            }
        }
    }
}

您應該將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);                
        }

    }

你在正確的軌道上,但是你需要把Console.ReadLine while循環中,並break循環,只有當用戶的值匹配的了。

像這樣的偽代碼:

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

do...while繼續要求用戶輸入新號碼之前,我會使用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)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM