如何使用C#中的方法存储变量?
我对下面的代码有一些问题。我还在学习,我不知道如何修复它 1.-我要做的是创建一个方法(GetInt)来存储变量,就像我在第二个方法(GetTrack)中尝试做的一样,它将进入我的主方法 2.-当存在无效输入时,我无法让GetInt方法循环,我猜try/catch和boolean之类的东西有问题 多谢各位如何使用C#中的方法存储变量?,c#,methods,boolean,try-catch,do-while,C#,Methods,Boolean,Try Catch,Do While,我对下面的代码有一些问题。我还在学习,我不知道如何修复它 1.-我要做的是创建一个方法(GetInt)来存储变量,就像我在第二个方法(GetTrack)中尝试做的一样,它将进入我的主方法 2.-当存在无效输入时,我无法让GetInt方法循环,我猜try/catch和boolean之类的东西有问题 多谢各位 //Get int Method static public void GetInt(string sPrompt, int iMin, int iMax) { int iNum;
//Get int Method
static public void GetInt(string sPrompt, int iMin, int iMax)
{
int iNum;
bool bError = false;
do
{
bError = true;
try
{
Console.Write(sPrompt);
iNum = int.Parse(Console.ReadLine());
if ((iNum < iMin) || (iNum > iMax))
{
Console.WriteLine("The value is out of range.");
bError = true;
}
}
catch (ArgumentException)
{
Console.WriteLine("An invalid number was entered, please try again.");
bError = true;
}
}
while (bError == false);
}
//Get Track Method
static public void GetTrack()
{
int iMin;
int iSec;
iMin = GetInt("Enter the minutes: ", 0, 10);
iSec = GetInt("Enter the seconds: ", 0, 59);
}
//获取int方法
静态公共void GetInt(字符串sPrompt、int-iMin、int-iMax)
{
国际单位制;
bool-bError=false;
做
{
bError=真;
尝试
{
Console.Write(sPrompt);
iNum=int.Parse(Console.ReadLine());
if((iNumiMax))
{
WriteLine(“值超出范围”);
bError=真;
}
}
捕获(异常)
{
Console.WriteLine(“输入的号码无效,请重试。”);
bError=真;
}
}
while(bError==false);
}
//追踪法
静态公共void GetTrack()
{
int iMin;
int iSec;
iMin=GetInt(“输入分钟:”,0,10);
iSec=GetInt(“输入秒:”,0,59);
}
在GetInt
的开头,您立即将bError
设置为true。这很可能是false,所以您实际上会循环,因为您没有将其设置为false
此外,您不会从该方法返回任何内容,因此不会返回任何内容。您必须将方法更改为返回
int
,并在获得它时实际返回值。您的GetInt
方法的声明应更改为以下内容:
//Get int Method
static public int GetInt(string sPrompt, int iMin, int iMax)
删除bError=true从do…while循环开始的代码>语句。
在do…while循环之后,添加以下语句:
return iNum;
此外,如果您的意图是不断提示用户直到输入被接受,则您的while
条件应该从bError==false
更改为bError==true
,或者简单地更改为bError
,这意味着相同的事情。以下是我如何从控制台“获取”用户输入的:
string choice = string.Empty;
bool goodChoice = false;
while (!goodChoice)
{
Console.Clear();
Console.WriteLine(string.Empty);
Console.WriteLine("Do you really want to hurt me?");
Console.WriteLine(string.Empty);
Console.WriteLine(string.Empty);
Console.WriteLine("Please Y or N (or 0 to exit)");
choice = Console.ReadLine().Trim();
if (choice.Equals("Y", StringComparison.OrdinalIgnoreCase))
{
goodChoice = true;
}
if (choice.Equals("N", StringComparison.OrdinalIgnoreCase))
{
goodChoice = true;
}
if (choice.Equals("0"))
{
goodChoice = true;
return; /* exist the routine */
}
}
根据你的情况修改
string choice = string.Empty;
bool goodChoice = false;
while (!goodChoice)
{
Console.Clear();
Console.WriteLine(string.Empty);
Console.WriteLine("Enter an Integer between {0} and {1}", minValue, maxValue);
Console.WriteLine(string.Empty);
Console.WriteLine(string.Empty);
Console.WriteLine("Please enter an integer (or X to exit)");
choice = Console.ReadLine().Trim();
int intParseResult = 0;
bool intParseAttempt = int.TryParse(choice, out intParseResult);
if(!intParseAttempt)
{
goodChoice = false;
}
else
{
if ((intParseResult < minValue) || (intParseResult > maxValue))
{
Console.WriteLine("Out of Range");
}
else
{
goodChoice = true;
}
}
if (choice.Equals("X"))
{
goodChoice = true;
return -99999; /* you'll have to figure out how to handle exits on your own */
}
}
string choice=string.Empty;
bool-goodChoice=false;
而(!goodChoice)
{
Console.Clear();
Console.WriteLine(string.Empty);
WriteLine(“输入一个介于{0}和{1}之间的整数”,minValue,maxValue);
Console.WriteLine(string.Empty);
Console.WriteLine(string.Empty);
WriteLine(“请输入一个整数(或X退出)”;
choice=Console.ReadLine().Trim();
int intParseResult=0;
bool intparsetrunt=int.TryParse(选项,out intParseResult);
如果(!intparsetrument)
{
好选择=假;
}
其他的
{
if((intParseResultmaxValue))
{
控制台。WriteLine(“超出范围”);
}
其他的
{
好选择=正确;
}
}
if(选择等于(“X”))
{
好选择=正确;
return-99999;/*您必须自己找出如何处理出口*/
}
}
在void
上快速搜索void方法的功能。。关于返回值
您的try
只包装iNum=int.Parse(Console.ReadLine())代码>以便您不会捕获(并忽略)其他异常。例如,如果sPrompt
为null
,则Console.Write
将引发异常,但您的程序只会捕获该异常并说“无效数字,再试一次”,即使该数字有效,它也是sPrompt
无效。请删除匈牙利符号。它和玛卡丽娜一起出去了。(例如:因努姆、iMax)