C#用户输入循环跳过代码
非常新的编码。我发布了一个关于如何在不破坏程序的情况下检查无效用户输入的问题,很快得到了回答,谢谢!现在,我已经将循环设置为拒绝除1-10之间的值以外的任何内容,并尝试让它转到下一个属性以进行输入,但不知道如何阻止它跳到代码末尾。这是我的密码:C#用户输入循环跳过代码,c#,loops,while-loop,C#,Loops,While Loop,非常新的编码。我发布了一个关于如何在不破坏程序的情况下检查无效用户输入的问题,很快得到了回答,谢谢!现在,我已经将循环设置为拒绝除1-10之间的值以外的任何内容,并尝试让它转到下一个属性以进行输入,但不知道如何阻止它跳到代码末尾。这是我的密码: UInt32 attributePoints = 25; bool validInput = false; string inputStrength; string inputSpeed;
UInt32 attributePoints = 25;
bool validInput = false;
string inputStrength;
string inputSpeed;
UInt32 validStrength = 0;
UInt32 validSpeed = 0;
while (!validInput)
{
Console.WriteLine("You have: " + attributePoints + " attribute points remaining to choose from");
Console.Write("Please enter a value between 1-10 for strength: ");
inputStrength = Console.ReadLine();
if (!UInt32.TryParse(inputStrength, out validStrength))
{
Console.WriteLine("Input was not a valid value for strength.");
}
else if (validStrength < 0 || validStrength > 10)
{
Console.WriteLine("Input was not a valid value for strength.");
}
else
{
validInput = true;
}
while (!validInput)
{
Console.Write("Please enter a value between 1-10 for speed: ");
inputSpeed = Console.ReadLine();
if (!UInt32.TryParse(inputSpeed, out validSpeed))
{
Console.WriteLine("Input was not a valid value for speed.");
}
else if (validSpeed < 0 || validSpeed > 10)
{
Console.WriteLine("Input was not a valid value for speed.");
}
else
{
validInput = true;
}
}
Console.WriteLine(String.Format("Strength Value = {0}", validStrength));
Console.WriteLine(String.Format("Speed Value = {0}", validSpeed));
}
UInt32属性点=25;
布尔有效输入=假;
字符串输入强度;
字符串输入速度;
UInt32有效强度=0;
UInt32有效速度=0;
而(!validInput)
{
WriteLine(“您还有:“+attributePoints+”属性点可供选择”);
控制台。写入(“请输入1-10之间的强度值:”);
inputStrength=Console.ReadLine();
如果(!UInt32.TryParse(输入强度,输出有效强度))
{
WriteLine(“输入不是有效的强度值。”);
}
否则如果(有效强度<0 | |有效强度>10)
{
WriteLine(“输入不是有效的强度值。”);
}
其他的
{
validInput=true;
}
而(!validInput)
{
控制台。写入(“请为速度输入1-10之间的值:”);
inputSpeed=Console.ReadLine();
如果(!UInt32.TryParse(输入速度、输出有效速度))
{
WriteLine(“输入不是速度的有效值。”);
}
否则如果(有效速度<0 | |有效速度>10)
{
WriteLine(“输入不是速度的有效值。”);
}
其他的
{
validInput=true;
}
}
WriteLine(String.Format(“强度值={0}”,validStrength));
WriteLine(String.Format(“速度值={0}”,validSpeed));
}
如果我运行程序时故意输入无效的速度输入,最终结果如下:
请为强度输入1-10之间的值:a
输入不是有效的强度值
请为速度输入一个介于1-10之间的值:5
强度值=0
速度值=5
我怎样才能让这段代码不断地询问强度值,直到给出一个有效的数字,然后再转到速度?谢谢大家! 将代码更改为:
UInt32 attributePoints = 25;
string inputStrength;
string inputSpeed;
UInt32 validStrength = 0;
UInt32 validSpeed = 0;
while (true)
{
Console.WriteLine("You have: " + attributePoints + " attribute points remaining to choose from");
Console.Write("Please enter a value between 1-10 for strength: ");
inputStrength = Console.ReadLine();
if (!UInt32.TryParse(inputStrength, out validStrength))
Console.WriteLine("Input was not a valid value for strength.");
else if (validStrength < 0 || validStrength > 10)
Console.WriteLine("Input was not a valid value for strength.");
else
break;
}
while (true)
{
Console.Write("Please enter a value between 1-10 for speed: ");
inputSpeed = Console.ReadLine();
if (!UInt32.TryParse(inputSpeed, out validSpeed))
Console.WriteLine("Input was not a valid value for speed.");
else if (validSpeed < 0 || validSpeed > 10)
Console.WriteLine("Input was not a valid value for speed.");
else
break;
}
Console.WriteLine(String.Format("Strength Value = {0}", validStrength));
Console.WriteLine(String.Format("Speed Value = {0}", validSpeed));
UInt32属性点=25;
字符串输入强度;
字符串输入速度;
UInt32有效强度=0;
UInt32有效速度=0;
while(true)
{
WriteLine(“您还有:“+attributePoints+”属性点可供选择”);
控制台。写入(“请输入1-10之间的强度值:”);
inputStrength=Console.ReadLine();
如果(!UInt32.TryParse(输入强度,输出有效强度))
WriteLine(“输入不是有效的强度值。”);
否则如果(有效强度<0 | |有效强度>10)
WriteLine(“输入不是有效的强度值。”);
其他的
打破
}
while(true)
{
控制台。写入(“请为速度输入1-10之间的值:”);
inputSpeed=Console.ReadLine();
如果(!UInt32.TryParse(输入速度、输出有效速度))
WriteLine(“输入不是速度的有效值。”);
否则如果(有效速度<0 | |有效速度>10)
WriteLine(“输入不是速度的有效值。”);
其他的
打破
}
WriteLine(String.Format(“强度值={0}”,validStrength));
WriteLine(String.Format(“速度值={0}”,validSpeed));