【问题标题】:Returning a value from a static void to a static void, that make use of public variables - some guidance please将使用公共变量的值从静态 void 返回到静态 void - 请提供一些指导
【发布时间】:2012-09-09 15:31:10
【问题描述】:

大家都知道我刚刚开始编写 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;
        }
    }
}

【问题讨论】:

  • 这不是“空洞”。它是一个函数。函数的返回类型是void,但将函数定义称为其返回类型的实例是不正确的。

标签: c# int return


【解决方案1】:

请阅读passing parameters by reference in C#

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

当您调用 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 语句。

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

【讨论】:

  • 谢谢。这帮了大忙。我不知道 ref 关键字。 code() 也使用 NumberChange() 因为它循环返回直到 exit 为 True。因此,如果用户决定更改数字范围,那么这将一直有效,直到退出为真。
  • @Inafune: 当然可以,但是数字范围的变化完全发生在Code() 内,与public static int frompublic static int to 无关。如果答案解决了您的疑问/问题,请将答案标记为已接受。
【解决方案2】:

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

把你的方法改成这样:

    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; 

【讨论】:

  • 只要不对Code()方法做同样的事情,这不会改变任何东西。
【解决方案3】:

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

类似的东西

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-12
    • 2016-07-03
    • 1970-01-01
    • 2011-09-23
    • 2012-11-06
    • 2012-10-18
    • 1970-01-01
    相关资源
    最近更新 更多