Tom*_*Tom 0 c# validation binary decimal
好的,我已经将一个基本的二进制转换器变成了十进制,我正在尝试验证用户输入,因此它只能是0或1,这在第一次工作正常,如果他们输入的值不正确它要求他们重新输入它,但是如果他们第二次输入错误值就会出现问题,我怎么会碰巧解决这个问题呢?或者喜欢将它循环回程序的特定部分?非常感谢,这是我的代码:
if (iBinaryNum1 == 1 || iBinaryNum1 == 0)
{
  Console.WriteLine("The binary value entered for integer 1 is correct");
}
else
{
  Console.WriteLine("The binary value entered for integer 1 is incorrect");
  Console.WriteLine("Please Re-enter this value");
  iBinaryNum1 = Convert.ToInt32(Console.ReadLine());
}
Run Code Online (Sandbox Code Playgroud)
    是的,我建议使用循环.例如(伪代码)
bool validValue = false;
while(!validValue)
{
  // Get input from the user
  // Print a message and set validValue
  // As soon as you set validValue to false the loop will break
}
// Your value will be valid here.
Run Code Online (Sandbox Code Playgroud)
另外,请注意Convert.ToInt32- 如果输入无效值,它会抛出异常.你可以看一下int.TryParse.
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           119 次  |  
        
|   最近记录:  |