C# 使用公共变量将值从静态void返回到静态void-请提供一些指导

C# 使用公共变量将值从静态void返回到静态void-请提供一些指导,c#,int,return,C#,Int,Return,大家都知道我刚刚开始写C#,这就是练习 我在网上找到了一个猜数字游戏的代码,并一直在努力改进基本游戏,以便提供反馈,用户可以更改数字。 我让程序工作,但想把'NumberChange'模块与'Code'方法分开 我在我的“GuessTheNumber游戏”中创建了一个Main()方法,执行“Code”方法。 问题是当我改变数字范围时,'NumberChange'模块不会改变'publicstaticint-from'或'publicstaticint-to'的值;因此,数字的范围保持不变 代码如

大家都知道我刚刚开始写C#,这就是练习

我在网上找到了一个猜数字游戏的代码,并一直在努力改进基本游戏,以便提供反馈,用户可以更改数字。 我让程序工作,但想把'NumberChange'模块与'Code'方法分开

我在我的“GuessTheNumber游戏”中创建了一个Main()方法,执行“Code”方法。 问题是当我改变数字范围时,'NumberChange'模块不会改变'publicstaticint-from'或'publicstaticint-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 Main()
{
代码(从、到、新_范围);
}
公共静态int new_range=0;
公共静态int from=1;
公共静态int为=10;
静态无效代码(int-from、int-to、int-new_范围)
{
//int from=1;//用户将从中猜测的范围。
//int to=10;//用户猜测的范围。
int guessedNumber;//这将保存用户猜测的值。
int Counter=0;//计数器用于计算用户的猜测次数。
int selection=0;//此值用于程序开始时的选择
//int new_range=0;
bool exit=false;
while(exit==false)
{
WriteLine(“您想做什么?”);
WriteLine(“1:改变猜测数字的范围?范围当前是从{0}到{1}。”,从,到);
控制台。WriteLine(“2:试着猜数字?”);
Console.WriteLine(“3:退出程序?”);
Console.WriteLine(“请输入一个数字:”);
if(int.TryParse(Console.ReadLine(),out selection))
{
如果(选择==2)
{
int randomNumber=new Random()。Next(from,to);//在(from,to)变量之间生成一个随机数。
Write(“数字在{0}和{1}之间。”,from,to);
while(true)
{
控制台。写(“猜一猜”);
if(int.TryParse(Console.ReadLine(),out guessedNumber))
{
if(猜测数==随机数)
{
WriteLine(“你猜对数字了!”);
如果(计数器<2)
{
WriteLine(“你只用一圈就猜到了数字!太棒了!”);
控制台。写线(“”);
}
其他的
{
WriteLine(“你猜到了”+计数器+“次数”);
控制台。写线(“”);
}
打破
}
其他的
{
WriteLine(“您的猜测太{0}.”,(guessedNumber>randomNumber)?“高”:“低”);
计数器=计数器+1;
}
}
其他的
{
WriteLine(“输入不是整数”);
}
}
//Console.WriteLine();
//控制台。WriteLine(“按任意键退出”);
//Console.ReadKey();
}
else if(选择==1)
{
数字更改(从,到,新的\u范围);
}
其他的
{
退出=真;
}
}
}
}
静态整数变化(整数从、整数到、整数新范围)
{
Console.WriteLine(“请输入猜测开始的数字”);
int.TryParse(Console.ReadLine(),超出新范围);
from=新的_范围;
WriteLine(“现在输入猜测将指向的数字。”);
int.TryParse(Console.ReadLine(),超出新范围);
to=新的_范围;
返回新的_范围;
}
}
}

如果通过引用传递变量。然后你传递它们的函数可以改变它们

像这样的

static int NumberChange(ref int from, ref int to, ref int new_range)
{
    //Your code here
}
您的代码方法也一样

请阅读

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

调用
NumberChange()
时也会发生同样的情况:参数(局部变量)
的值从
复制到
NumberChange()
方法的参数中,并具有相同的名称,但如果这些值在
NumberChange
中修改,新的价值观再也回不来了;您刚刚将仅存在于相应方法中的局部变量
修改为

static int NumberChange(ref int from, ref int to, int new_range)
NumberChange(ref from, ref to, new_range);
    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;