0

嘿,所以我已经拥有了猜谜游戏所需的一切,并且一切都检查并运行没有问题,唯一的问题是我想要一个 wincount 和一个猜测计数,这样用户就不能得到比什么更多的猜测程序规定。目前该程序会告诉您应该得到多少猜测,但由于单个 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();
                }
            }
        }
    }
4

1 回答 1

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);
于 2013-10-02T15:50:34.247 回答