1

就这样每个人都知道我刚刚开始编写 C#,这是练习。

我在互联网上找到了一个 GuessTheNumberGame 代码,并一直在尝试改进基本游戏以便给出反馈,并且用户可以更改数字。我让程序正常工作,但想将“NumberChange”模块与“代码”方法分开。

我在“GuessTheNumberGame”中创建了一个 Main() 方法,并执行“代码”方法。问题是当我去更改数字范围时,“NumberChange”模块不会更改“Public Static int from”或“Public static int to”的值;因此,数字的范围保持不变。

下面的代码:

using System;

namespace GuessThatNumber
{
    class GuessTheNumberGame
    {
        static void Main()
        {
            Code(from, to, new_range);
        }

        public static int new_range = 0;
        public static int from = 1;
        public static int to = 10;

         static void Code(int from, int to, int new_range)
        {
            //int from = 1;   // The range the user will guess from.
            //int to = 10;    //The range the user will guess to.


            int guessedNumber;  //This will hold the value that the user guessed.
            int Counter = 0;    //Counter is used to count the number of guesses the user makes.
            int selection = 0;  //This value is for the selection at the start of the program
           //int new_range = 0;
            bool exit = false;

            while (exit == false)
            {
                Console.WriteLine("What would you like to do?");
                Console.WriteLine("1: Alter the range of guessed numbers? The range is currently from {0} to {1}.", from, to);
                Console.WriteLine("2: Try to guess the number?");
                Console.WriteLine("3: Exit the program?");
                Console.WriteLine("Please enter a number:");
                if (int.TryParse(Console.ReadLine(), out selection))
                {
                    if (selection == 2)
                    {
                        int randomNumber = new Random().Next(from, to);   //Generates a random number between the (from, to) variables.
                        Console.Write("The number is between {0} and {1}. ", from, to);
                        while (true)
                        {
                            Console.Write("Make a guess: ");
                            if (int.TryParse(Console.ReadLine(), out guessedNumber))
                            {
                                if (guessedNumber == randomNumber)
                                {
                                    Console.WriteLine("You guessed the right number!");
                                    if (Counter < 2)
                                    {
                                        Console.WriteLine("You guessed the number in only 1 turn! Amazing!");
                                        Console.WriteLine(" ");
                                    }
                                    else
                                    {
                                        Console.WriteLine("You guessed " + Counter + " times.");
                                        Console.WriteLine(" ");
                                    }
                                    break;
                                }
                                else
                                {
                                    Console.WriteLine("Your guess was too {0}.", (guessedNumber > randomNumber) ? "high" : "low");
                                    Counter = Counter + 1;
                                }
                            }
                            else
                            {
                                Console.WriteLine("Input was not an integer.");
                            }
                        }
                        //Console.WriteLine();
                        //Console.WriteLine("Press any key to exit.");
                        //Console.ReadKey();
                    }
                    else if (selection == 1)
                    {
                        NumberChange(from, to, new_range);
                    }

                    else
                    {
                        exit = true;
                    }
                }
            }
        }

        static int NumberChange(int from, int to, int new_range)
        {
            Console.WriteLine("Please enter the number that the guess will start from.");
            int.TryParse(Console.ReadLine(), out new_range);
            from = new_range;
            Console.WriteLine("Now enter the number that the guess will go to.");
            int.TryParse(Console.ReadLine(), out new_range);
            to = new_range;
            return new_range;
        }
    }
}
4

3 回答 3

0

请阅读有关在 C# 中通过引用传递参数的信息

到目前为止,您的参数是按值传递的。这意味着在程序开始调用时,和Code()的当前值被复制到参数和中。public static int frompublic static int tofromto

调用 时也会发生同样的情况NumberChange():参数(局部变量)的值fromto复制具有NumberChange()相同名称的方法的参数中,但是如果在 内部修改了这些值NumberChange,则新值永远不会从那里返回;您刚刚修改了局部变量fromto并且仅存在于相应的方法中。

相反,您可以使用ref关键字声明参数:

static int NumberChange(ref int from, ref int to, int new_range)

这意味着您也必须使用ref相应参数的关键字来调用您的方法:

NumberChange(ref from, ref to, new_range);

另外,请注意有关您的NumberChange()方法的两个问题:

  • 你传入一个new_range参数,但你不使用它。实际上,它在您对TryParse(). 因此,您可以简单地声明int new_range为局部变量,而不是将其作为参数传递。
  • 您从该方法返回一个值,但您不使用该值。因此,您可以将您的方法声明为void并删除该return语句。

最后,如果您希望您的public static int frompublic static int to变量由 更改,请与 类似地Code()添加关键字。但是,这对您当前的代码并没有真正有用,因为程序在离开后立即结束,并且根本不会使用新值。refNumberChange()Code()

于 2012-09-17T12:16:55.287 回答
0

您正在使用静态变量,然后将它们传递给已经可以访问它们的方法。将它们传入本质上会用本地版本覆盖静态版本。

将您的方法更改为:

    private static void NumberChange() 
    { 
        int new_range;

        Console.WriteLine("Please enter the number that the guess will start from."); 
        int.TryParse(Console.ReadLine(), out new_range); 
        from = new_range; 
        Console.WriteLine("Now enter the number that the guess will go to."); 
        int.TryParse(Console.ReadLine(), out new_range); 
        to = new_range; 
    } 


    static void Code()
    {


    }

并删除这一行:

public static int new_range = 0; 
于 2012-09-17T12:27:28.333 回答
-1

如果您通过引用传递变量。然后您传递它们的功能可以更改它们。

像这样的东西

static int NumberChange(ref int from, ref int to, ref int new_range)
{
    //Your code here
}

为您的 Code 方法添加相同的内容

于 2012-09-17T12:15:11.977 回答