【问题标题】:How would i go about creating a win count and guess count for my random number guessing game C#我将如何为我的随机数猜谜游戏 C# 创建获胜次数和猜测次数
【发布时间】:2013-10-02 15:47:53
【问题描述】:

嘿,所以我已经拥有了猜谜游戏所需的一切,并且一切都检查并运行没有问题,唯一的问题是我想要一个获胜次数和猜测次数,这样用户就无法获得更多猜测比程序规定的。目前该程序会告诉你应该得到多少猜测,但由于单个 if 语句,它只会让你猜测一次,那么我将如何去做呢?

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

namespace GuessingGame
{
    class Program
    {
        static void Main(string[] args)
        {
            // Declare variables
            Int32 currentGuess, guessCount, winCount, upperLimit, randomNumber;
            double maxGuesses;
            bool gameOver;
            char playAgain;
            Random random = new Random();



            // Display title
            Console.WriteLine("Welcome to the high/low guessing game.");
            //Request user input for upper limit
            Console.WriteLine("Enter Upper range (e.g. 100):");
            upperLimit = Int32.Parse(Console.ReadLine());

            //Generate Random Number
            randomNumber = random.Next(1, upperLimit);
            maxGuesses = Math.Ceiling(Math.Log(upperLimit, 2) - 1);

            // Begin game
            Console.WriteLine("I picked a number between 1 and {0} you get {1} chances to guess it", upperLimit, maxGuesses);

            // Begin Guessing Process

            //Guess #1
            {
                Console.WriteLine(" Enter Guess #1: ");
                currentGuess = Int32.Parse(Console.ReadLine());

                if (currentGuess == randomNumber)
                {
                    Console.WriteLine("You got it!");
                }
                if (currentGuess > randomNumber)
                {
                    Console.WriteLine("Too High");
                }
                if (randomNumber > currentGuess)
                {
                    Console.WriteLine("Too Low");
                }

                    Console.ReadLine();
                }
            }
        }
    }

【问题讨论】:

    标签: c# count console counter


    【解决方案1】:
           int gessNum = 0;
           do
           {
                if (gessNum++ == maxGuesses){
                    Console.WriteLine("You lost");
                    break;
                }
                Console.WriteLine(string.Format(" Enter Guess {0}: ", gessNum));
                currentGuess = Int32.Parse(Console.ReadLine());
    
                if (currentGuess == randomNumber)
                {
                    Console.WriteLine("You got it!");
                }
                if (currentGuess > randomNumber)
                {
                    Console.WriteLine("Too High");
                }
                if (randomNumber > currentGuess)
                {
                    Console.WriteLine("Too Low");
                }
                Console.ReadLine();
            } while (currentGuess != randomNumber);
    

    【讨论】:

    • 谢谢,我正在寻找更多建议,但我可以分析这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多