c#-控制台应用程序-解决用户在没有任何数字的情况下按enter键时出现的错误
标题是有点混乱,关于得到什么我想要在那里,这是我第一次在这里 基本上,我已将字符串转换为int:c#-控制台应用程序-解决用户在没有任何数字的情况下按enter键时出现的错误,c#,console-application,C#,Console Application,标题是有点混乱,关于得到什么我想要在那里,这是我第一次在这里 基本上,我已将字符串转换为int: string _val = Console.ReadLine(); tempScoreToWin = Convert.ToInt32(_val); 我想知道的是,当用户a在没有输入值的情况下按enter键时,会发生错误,应用程序将结束 我怎样才能避开这件事 这是我的全部代码: while (true) {
string _val = Console.ReadLine();
tempScoreToWin = Convert.ToInt32(_val);
我想知道的是,当用户a在没有输入值的情况下按enter键时,会发生错误,应用程序将结束
我怎样才能避开这件事
这是我的全部代码:
while (true)
{ //This will allow the player to manually change the score.
//------------------------------------------------------------------------------------------
string _val = "";
ConsoleKeyInfo key;
do
{
key = Console.ReadKey(true);
if (key.Key != ConsoleKey.Backspace)
{
double val = 0;
bool _x = double.TryParse(key.KeyChar.ToString(), out val);
if (_x)
{
_val += key.KeyChar;
Console.Write(key.KeyChar);
}
}
else
{
if (key.Key == ConsoleKey.Backspace && _val.Length > 0)
{
_val = _val.Substring(0, (_val.Length - 1));
Console.Write("\b \b");
}
}
}
while (key.Key != ConsoleKey.Enter);
Console.WriteLine();
//-----------------------------------------------------------------
tempScoreToWin = Convert.ToInt32(_val); // Converting the users input (Console.ReadLine()) into an integer.
if (tempScoreToWin > 0) // If the users input is higher than zero ...
{
scoreToWin = tempScoreToWin; // Reset the scoreToWin variable with the value of tempScoreToWin.
Console.WriteLine("The score has been set to {0}.", scoreToWin); // Let the user know that the score has been changed successfully.
break; // Break out of the while loop.
}
else
{ // If the player has not put a correct integer in ...
Console.WriteLine("The score has been set at a default of {0}.", scoreToWin); // then the score will be set to the default value of scoreToWin
break; // Break out of the while loop.
}
}
Console.ReadLine();
Console.Clear();
//-----------------------------------------------------------------
干杯 使用
TryParse
可以将字符串解析为整数,同时检查是否成功
if(int.TryParse(_val, out tempScoreToWin)
{
//Parse succeeded
}
else
{
//Parse failed
}
太棒了!这很管用,赞德先生。非常感谢你们的快速回答。我真的很感激:)
看看
Int32.TryParse
方法。你想做什么呢?编辑器中还有一个类似{}
的按钮,允许你将文章的部分标记为代码。我会编辑代码块来整理它们,但是你现在看起来像是在积极地编辑你的帖子,所以不想意外地丢失你正在做的任何更改。嗨,弹出一条无效消息或其他什么,告诉玩家再次输入一个数字。。。还有,我如何让我的代码正确地显示在这里?你为什么要自己处理退格?只需使用Console.Readline
然后使用TryParse
。
if (int.TryParse(_val, out tempScoreToWin))
{
if (tempScoreToWin > 0) // If the users input is higher than zero ...
{
scoreToWin = tempScoreToWin; // Reset the scoreToWin variable with the value of tempScoreToWin.
Console.WriteLine("The score has been set to {0}.", scoreToWin); // Let the user know that the score has been changed successfully.
break; // Break out of the while loop.
}
else
{ // If the player has not put a correct integer in ...
Console.WriteLine("The score has been set at a default of {0}.", scoreToWin); // then the score will be set to the default value of scoreToWin
break; // Break out of the while loop.
}
}
else
{
//Parse failed
}