简体   繁体   English

如何限制控制台输入C#

[英]How to limit console input c#

I am trying to limit the input of the console in order to add an error message on invalid input. 我试图限制控制台的输入,以便在无效输入上添加错误消息。 The input must be 9 numbers (Edit : In a range of 100000000 - 999999999) for it to be valid. 输入必须为9个数字(编辑:范围为100000000-999999999),该输入才有效。 The problem I am running into is getting the error to display properly as well as getting the program to continue to function. 我遇到的问题是使错误正确显示以及使程序继续运行。 The program is set up to work with sin as an integer. 该程序设置为使用sin作为整数。

int sin;
Console.Write("Please enter sin number: ");
sin = int.Parse(Console.ReadLine());

I tried using this below but got endless error messages as well as invalid input on all inputs 我尝试在下面使用它,但收到无尽的错误消息以及所有输入上的输入无效

var digits = Console.ReadLine();
while (digits.Length > 9 || digits.Length < 9)
{
    Console.WriteLine("Error invalid entry");
}

How do I get the program to check the length of the digits as well as continue the program with sin as an int value so the rest of the program can work properly? 如何获取程序以检查数字的长度以及以sin作为int值继续执行程序,以便程序的其余部分可以正常工作?

you need to read again. 您需要重新阅读。 you're simply printing there's an error, but not prompting to get a new input with a new sin = int.Parse(Console.ReadLine()); 您只是在打印一个错误,但是没有提示输入带有新sin = int.Parse(Console.ReadLine());的新输入sin = int.Parse(Console.ReadLine());

The following should work (edited, cheers ben) 以下应该工作(编辑,加油)

int sin;
Console.Write("Please enter sin number: ");
string input = Console.ReadLine();
bool parse_ok=  int.TryParse(input,out sin);

while (!parse_ok || digits.Length != 9 || sin < 100000000 )
{
    Console.WriteLine("Error invalid entry");
    Console.Write("Please enter sin number: ");
    input = Console.ReadLine();  
    parse_ok=  int.TryParse(input,out sin);
}   ;

Your Console.ReadLine() is outside the loop, so once you get to the error message there's no way for the user to enter a new value; 您的Console.ReadLine()不在循环中,因此一旦出现错误消息,用户将无法输入新值。 the error message just displays in an endless loop. 错误消息只会无限循环显示。 Try this instead - once you're safely out of the loop due to good input the sin variable will have the integer value of the SIN: 尝试以下方法-一旦由于良好的输入而安全地退出循环, sin变量将具有SIN的整数值:

int sin = 0;
while (true) {
    String digits = Console.ReadLine();
    if (digits.Length == 9 && Int32.TryParse(digits, out sin)) {
        if (sin >= 100000000) {
            break;
        }
    }
    Console.WriteLine("Error invalid entry");
}

You just need a series of three checks: 您只需要一系列的三项检查:

  1. The length of the input is 9 输入的长度为9
  2. The input can successfully parsed to an integer int.TryParse(string, out int) 输入可以成功解析为整数int.TryParse(string, out int)
  3. The parsed integer is in the range of 100000000 - 999999999 解析的整数在100000000-999999999的范围内

With that, you can try the following: 这样,您可以尝试以下操作:

Updated to include an error message 更新以包含错误消息

using System;

public class Program
{
    public static void Main()
    {
        int sin = 0;

        Console.Write("Please enter sin number: ");
        string input = Console.ReadLine();
        while(input.Length != 9 ||
              !int.TryParse(input, out sin) || 
              sin < 100000000)
        {
            Console.WriteLine("Error invalid entry!");
            Console.WriteLine("");

            Console.Write("Please enter sin number: ");
            input = Console.ReadLine();
        }

        Console.WriteLine("Entered number: {0}", sin);
    }
}

Results: 结果:

Please enter sin number: 012345678
Error invalid entry!

Please enter sin number: 0123456789
Error invalid entry!

Please enter sin number: asdf
Error invalid entry!

Please enter sin number: 999999999
Entered number: 999999999

See working example here... https://dotnetfiddle.net/eHNy2O 在这里查看工作示例... https://dotnetfiddle.net/eHNy2O

This should work 这应该工作

var digits = int.Parse(Console.ReadLine());

while (digits.Length > 9 || digits.Length < 9)
{
    Console.WriteLine("Error invalid entry");
    // then add something like this
    Console.Write("Enter a number: ");
    digits = int.Parse(Console.ReadLine());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM