代码将无法编译,我不明白这些错误是什么

时间:2013-10-11 01:48:19

标签: c# syntax-error console-application

因为标题说我的代码将无法编译我现在得到多个问题,之前它的格式不同,它在某种程度上起作用。但现在代码完全拒绝运行,并且凭借我目前的理解水平,我无法弄清楚问题,但我相信问题只是我格式化程序的方式,因此它遵循流程图。

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

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            // SETUP SESSION
            // Declare variables
            Int32 currentGuess, upperLimit, randomNumber;
            double maxGuesses;
            char playAgain;
            Random random = new Random();


            // WELCOME THE USER
            Console.WriteLine("Welcome to the high/low guessing game.");

            //INITILIZE WIN COUNT AND GAME COUNT

            Int32 gameCount = 0, winCount = 0;

            //DO LOOP #1
            do
            //SETUP GAME
            {
            //REQUEST USER FOR UPPER LIMIT
                Console.WriteLine("Enter Upper range (e.g. 100):");
                upperLimit = Int32.Parse(Console.ReadLine());

                //INITILIZE GUESS COUNT
                Int32 guessCount = 0;
                bool gameOver = false;

                //DETERMINE RANDOM NUMBER
                randomNumber = random.Next(1, upperLimit);
                //DETERMINE MAX GUESSES
                maxGuesses = Math.Ceiling(Math.Log(upperLimit, 2) - 1);

                // INFORM USER RANDOM NUMBER IS CHOSEN AND INDICATE NUMBER OF GUESSES ALLOWED 
                Console.WriteLine("I picked a number between 1 and {0} you get {1} chances to guess it", upperLimit, maxGuesses);

            //DO LOOP #2
            do
                            {
                                //PLAY GAME
                               //READ GUESSES
                            Console.WriteLine(string.Format(" Enter Guess {0}: ", guessCount));
                            currentGuess = Int32.Parse(Console.ReadLine());
                            if (currentGuess == randomNumber)
                            {
                                //INCRIMENT WIN COUNT
                                winCount++;
                            }
                            if (currentGuess == randomNumber)
                            {
                                Console.WriteLine("You got it!");
                                gameOver = true;
                                gameCount++;
                            }
                            else if (currentGuess > randomNumber)
                            {
                                Console.WriteLine("Too High");
                            }
                            else if (currentGuess < randomNumber)
                            {
                                Console.WriteLine("Too Low");
                            }


                            } while (guessCount < maxGuesses &&  gameOver == false);

                //POST PROCESSING GAME

                             if (guessCount++ == maxGuesses)
                                        {
                                            //INCRIMENT GAME COUNT
                                            gameCount++;
                                            Console.WriteLine("You lost");
                                            //DISPLAY CORRECT NUMBER IF TOO MANY INCORRECT GUESSES
                                            Console.WriteLine("\nThe number was {0},better luck next time!", randomNumber);
                                            guessCount = 1;

                                 //PROMPT TO PLAY AGAIN

                                           Console.WriteLine("Would you like to play again? (Y/N)");
                                           playAgain = char.Parse (Console.ReadLine());


                                       }
                                           while (playAgain == 'y' ||playAgain == 'Y');

                          // display win count 
                         Console.WriteLine("Thanks for playing, you won {0} out of {1} games", winCount, gameCount);
                         Console.ReadLine();
       }
    }
}

2 个答案:

答案 0 :(得分:1)

两个问题:

  1. 你在外面做的最后一次循环之前,你错过了一个松散的支架。
  2. 您需要在声明时初始化“playAgain”字符。
  3. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication3
    {
        class Program
        {
             static void Main(string[] args)
             {
                // SETUP SESSION
                // Declare variables
                Int32 currentGuess, upperLimit, randomNumber;
                double maxGuesses;
                char playAgain = 'n';
                Random random = new Random();
    
                // WELCOME THE USER
                Console.WriteLine("Welcome to the high/low guessing game.");
    
                //INITILIZE WIN COUNT AND GAME COUNT
    
                Int32 gameCount = 0, winCount = 0;
    
                //DO LOOP #1
                do
                //SETUP GAME
                {
                    //REQUEST USER FOR UPPER LIMIT
                    Console.WriteLine("Enter Upper range (e.g. 100):");
                    upperLimit = Int32.Parse(Console.ReadLine());
    
                    //INITILIZE GUESS COUNT
                    Int32 guessCount = 0;
                    bool gameOver = false;
    
                    //DETERMINE RANDOM NUMBER
                    randomNumber = random.Next(1, upperLimit);
                    //DETERMINE MAX GUESSES
                    maxGuesses = Math.Ceiling(Math.Log(upperLimit, 2) - 1);
    
                    // INFORM USER RANDOM NUMBER IS CHOSEN AND INDICATE NUMBER OF GUESSES ALLOWED 
                    Console.WriteLine("I picked a number between 1 and {0} you get {1} chances to guess it", upperLimit, maxGuesses);
    
                    //DO LOOP #2
                    do
                    {
                        //PLAY GAME
                        //READ GUESSES
                        Console.WriteLine(string.Format(" Enter Guess {0}: ", guessCount));
                        currentGuess = Int32.Parse(Console.ReadLine());
                        if (currentGuess == randomNumber)
                        {
                            //INCRIMENT WIN COUNT
                            winCount++;
                        }
                        if (currentGuess == randomNumber)
                        {
                            Console.WriteLine("You got it!");
                            gameOver = true;
                            gameCount++;
                        }
                        else if (currentGuess > randomNumber)
                        {
                            Console.WriteLine("Too High");
                        }
                        else if (currentGuess < randomNumber)
                        {
                            Console.WriteLine("Too Low");
                        }
    
    
                    } while (guessCount < maxGuesses && gameOver == false);
    
                    //POST PROCESSING GAME
    
                    if (guessCount++ == maxGuesses)
                    {
                        //INCRIMENT GAME COUNT
                        gameCount++;
                        Console.WriteLine("You lost");
                        //DISPLAY CORRECT NUMBER IF TOO MANY INCORRECT GUESSES
                        Console.WriteLine("\nThe number was {0},better luck next time!", randomNumber);
                        guessCount = 1;
    
                        //PROMPT TO PLAY AGAIN
    
                        Console.WriteLine("Would you like to play again? (Y/N)");
                        playAgain = char.Parse(Console.ReadLine());
    
    
                    }
                } while (playAgain == 'y' || playAgain == 'Y');
    
                // display win count 
                Console.WriteLine("Thanks for playing, you won {0} out of {1} games", winCount, gameCount);
                Console.ReadLine();
           }
        }
    }
    

答案 1 :(得分:0)

您的计划中有两个错误。首先,在使用变量之前,必须始终初始化变量。如果您查看playAgain变量可以采用的不同路径,则只会在if (guessCount++ == maxGuesses)路径中设置该值。如果条件为假,您将无法获得while部分。您可以将声明更改为

char playAgain = 'n';

第二个错误是缺少括号。如果你仔细地做缩进,你会看到:

do
{
    //Bunch of code
    if (guessCount++ == maxGuesses)
    {
        //INCRIMENT GAME COUNT
        gameCount++;
        Console.WriteLine("You lost");
        //DISPLAY CORRECT NUMBER IF TOO MANY INCORRECT GUESSES
        Console.WriteLine("\nThe number was {0},better luck next time!", randomNumber);
        guessCount = 1;

        //PROMPT TO PLAY AGAIN

        Console.WriteLine("Would you like to play again? (Y/N)");
        playAgain = char.Parse(Console.ReadLine());

} while (playAgain == 'y' || playAgain == 'Y');

正如您所看到的,您错过了if括号,它应该是

   playAgain = char.Parse(Console.ReadLine());
   }

} while (playAgain == 'y' || playAgain == 'Y');

此外,还有 LOT 你可以改进的东西。例如,您应该将变量声明置于不使用它们的位置。每条评论应该是一个单独的方法;如果您的Main,而不是一大堆代码,那么您将更容易遵循流程图:

private static void Main(string[] args)
{
    WelcomeTheUser();
    InitializeWinCountAndGameCount();
    Play();
    DisplayWinCount();
}

如果你的功能很小,你也可以避免使用大量错位的支架。