所以每个人都知道我已经开始编写C#了,这就是练习。
我在互联网上找到了一个GuessTheNumberGame代码,并且一直在尝试改进基本游戏,以便给出反馈,用户可以更改数字。 我让程序正常工作,但想将'NumberChange'模块与'Code'方法分开。
我在'GuessTheNumberGame'中创建了一个Main()方法,并执行'Code'方法。 问题是当我去改变数字范围时,'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;
}
}
}
答案 0 :(得分:0)
请阅读passing parameters by reference in C#。
到目前为止,您的参数都是按值传递的。这意味着,在程序开头调用Code()
时,public static int from
和public static int to
的当前值被复制到参数from
中to
。
调用NumberChange()
时会发生同样的情况:参数(局部变量)from
和to
的值被复制到{的参数中{1}}具有相同名称的方法,但如果在NumberChange()
内修改了这些值,则新值不会从那里返回;您刚刚修改了仅存在于相应方法中的局部变量NumberChange
和from
。
相反,您可以使用to
关键字声明参数:
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 from
变量由public static int to
更改,请将Code()
关键字类似地添加到ref
。但是,这对您当前的代码并不是很有用,因为程序在离开NumberChange()
后立即结束,并且根本不会使用新值。
答案 1 :(得分: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;
答案 2 :(得分:-1)
如果您通过引用传递变量。然后你传递它们的功能可以改变它们。
类似这样的事情
static int NumberChange(ref int from, ref int to, ref int new_range)
{
//Your code here
}
为您的代码方法添加相同的内容