如何摆脱goto的?

时间:2016-07-21 03:12:55

标签: c#

我是编程新手,我想知道在我的代码中是否有办法不使用goto

using System;

namespace project1

{
    class MainClass
{
    public static void Main()
    {
        start:

        Console.Clear ();

        Console.ForegroundColor = ConsoleColor.Green;

        int rannum;
        int input;

        Random r = new Random ();
        rannum = r.Next (1, 1001);

        Console.WriteLine ("Guess the number!");

        game:
        input = Convert.ToInt32 (Console.ReadLine ());

        if (input == rannum) 
        {
            Console.WriteLine ("Congrats!");
            Console.WriteLine ("Press any key to go again...");
            Console.ReadKey(true);
            goto start;
        } 
        else if (input <= rannum)
        {
            Console.WriteLine ("Guess higher!");
            goto game;
        } 
        else if (input >= rannum)
        {   Console.WriteLine ("Guess lower!");
            goto game;
        }
    }
}

我知道goto很糟糕(显然),所以如果没有它们我该如何做?

2 个答案:

答案 0 :(得分:3)

Jon在我形成这个答案时发表了评论,显然很有思想的人都这么认为。

无论如何,这是你如何消除goto start

public static void Main()
{
     Random r = new Random ();
     string again;
     do {
         PlayOneGame( r.Next(1, 1001) );
         Console.WriteLine("Play again?");
         again = Console.ReadLine();
     } while (!again.ToLower().StartsWith('n'));
}

public static void PlayOneGame(int rannum)
{
    Console.Clear ();

    Console.ForegroundColor = ConsoleColor.Green;

    int input;



    Console.WriteLine ("Guess the number!");

    game:
    input = Convert.ToInt32 (Console.ReadLine ());

    if (input == rannum) 
    {
        Console.WriteLine ("Congrats!");
        return;  // goes back to the caller, Main, but without using goto
    } 
    else if (input <= rannum)
    {
        Console.WriteLine ("Guess higher!");
        goto game;
    } 
    else if (input >= rannum)
    {   Console.WriteLine ("Guess lower!");
        goto game;
    }
}

现在,你能以类似的方式摆脱goto game吗?

请注意,我还安排了只创建一个Random对象的东西,并且无论玩了多少游戏都继续使用它 - 继续创建新的RNG并不是一个好主意,起始状态是不是非常随意。

答案 1 :(得分:0)

我刚刚做了这样的事情并且正在工作,我还在做错什么吗?

public static void Main ()
    {
        PlayGame ();

        Console.WriteLine ("Press 'n' key to exit the application, press any other key to play again.");
        string again = Convert.ToString(Console.ReadKey ());
        if (again == "n") {
            Environment.Exit (0);
        }

        PlayGame ();
    }

    public static void PlayGame ()
    {
        Console.Clear ();

        Console.ForegroundColor = ConsoleColor.Cyan;

        int randomNumber;
        int userInput;
        int gameover = 0;

        Random number = new Random ();
        randomNumber = number.Next (1, 1001);

        Console.WriteLine ("Guess the number!");

        while (gameover != 1) 
        {
            userInput = Convert.ToInt32(Console.ReadLine());

            if (userInput == randomNumber) {
                Console.Clear ();
                Console.WriteLine ("Congrats!");
                return;
            } else if (userInput <= randomNumber) {
                Console.Clear ();
                Console.WriteLine ("Guess Higher!");
            } else if (userInput >= randomNumber) {
                Console.Clear ();
                Console.WriteLine ("Guess Lower!");
            }
        }
    }