C# 触发方法c之前需要验证一组用户输入#

C# 触发方法c之前需要验证一组用户输入#,c#,validation,C#,Validation,在触发方法并将数据存储到数据库之前,我需要检查控制台应用程序中的一组用户输入 程序编译和运行时没有异常。但在一个输入错误的情况下,它仍然会对其他三个输入执行。 尽管如此,我真正需要的是在触发方法之前确保4个用户的输入是正确的,如果只有一个错误,整个程序应该停止并退出 using System; using System.Threading; namespace BarcodeValidation { class Program { static void Mai

在触发方法并将数据存储到数据库之前,我需要检查控制台应用程序中的一组用户输入

程序编译和运行时没有异常。但在一个输入错误的情况下,它仍然会对其他三个输入执行。
尽管如此,我真正需要的是在触发方法之前确保4个用户的输入是正确的,如果只有一个错误,整个程序应该停止并退出

using System;
using System.Threading;

namespace BarcodeValidation
{
    class Program
    {
        static void Main(string[] args)
        {
            ReadBarcode();
        }

        static void ReadBarcode()
        {
            var barcodes = GetInput();

            foreach (var item in barcodes)
            {
                // something
                CheckUserInput(item);
            }
        }

        static string[] GetInput()
        {
            Console.WriteLine("Please enter 4 products ID, Barcodes, MPN or EAN code:");

            string[] barcode = new string[4];

            for (int i = 0; i < barcode.Length; i++)
            {
                barcode[i] = Console.ReadLine();
            }
            return barcode;
        } // end of method here

        static void CheckUserInput(string userInput)
        {
            int msec = 5000;

            try
            {
                if (!(userInput == "F5121" || userInput == "F3111" || userInput == "F8331" || userInput == "F5321"))
                {
                    Console.WriteLine("Enter a valid MPN codes for your products");
                    Thread.Sleep(msec);
                    Environment.Exit(0);
                }
                else
                {
                    switch (userInput)
                    {
                        case "F5121":
                            Console.WriteLine("barcode 1 is =", userInput);
                            Thread.Sleep(msec);
                            break;
                        case "F3111":
                            Console.WriteLine("barcode 2 is =", userInput);
                            Thread.Sleep(msec);
                            break;
                        case "F8331":
                            Console.WriteLine("barcode 3 is =", userInput);
                            Thread.Sleep(msec);
                            break;
                        case "F5321":
                            Console.WriteLine("barcode 4 is =", userInput);
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}     
使用系统;
使用系统线程;
名称空间条码验证
{
班级计划
{
静态void Main(字符串[]参数)
{
ReadBarcode();
}
静态void ReadBarcode()
{
var条形码=GetInput();
foreach(条形码中的var项目)
{
//某物
检查用户输入(项目);
}
}
静态字符串[]GetInput()
{
Console.WriteLine(“请输入4个产品ID、条形码、MPN或EAN代码:”);
字符串[]条形码=新字符串[4];
对于(int i=0;i
您可以使用的一个选项是创建自己的类,该类派生自
System.Exception
,如果发现其中一个输入无效,您可以抛出异常类的实例


您可以将代码包装在一个
try catch
块中,然后将修正代码放入
catch
块中。

因为您有一个方法可以实际测试用户输入使用它的返回值:

static bool CheckUserInput(string userInput) // true : valid | false : invalid
{
    int msec = 5000;
    try
    {
        if (!(userInput == "F5121" || 
              userInput == "F3111" || 
              userInput == "F8331" || 
              userInput == "F5321"))
        {     
            Console.WriteLine("Enter a valid MPN codes for your products");
            return false;
        }
        else
        {
            switch (userInput)
            {
                case "F5121":
                    Console.WriteLine("barcode 1 is =", userInput);
                    Thread.Sleep(msec);
                    return true;                      
                case "F3111":
                    Console.WriteLine("barcode 2 is =", userInput);
                    Thread.Sleep(msec);
                    return true;
                case "F8331":
                    Console.WriteLine("barcode 3 is =", userInput);
                    Thread.Sleep(msec);
                    return true;
                case "F5321":
                    Console.WriteLine("barcode 4 is =", userInput);
                    return true;
                default:
                    return false;
            }
        }

    }
    catch (Exception ex)
    {

        Console.WriteLine(ex.Message);
        return false;
    }

} 
读条形码可能如下所示:

static void ReadBarcode()
{
    var barcodes = GetInput();
    bool errorOccured = false;
    foreach (var item in barcodes)
    {
        // something
        if(!CheckUserInput(item))
        {
            errorOccured = true; // keep track of that error
            break; //Break for if 1 input is invalid
        }
    }
    //Further execution....
    if(errorOccured)
    {
        return; //Do not continue ...
    }
    //Do other things you want to do. Your input is valid at this point !
}
或更短,如默认报价:

static void ReadBarcode()
{        
    if(!GetInput().All(CheckUserInput))
    {
        return;
    }
    //Your stuff goes here. Input is valid at this point
}

您需要将检查代码和“输出”代码分解到不同的位置。您需要检查所有值是否都是有效值。检查完所有值后,再执行console.writelines(这是您不希望发生的部分)。此时,它检查一个,如果该代码有效,则执行该代码,然后继续执行下一个代码
CheckUserInput
实际上应该只检查用户输入,而不应该根据该方法的结果执行您想要限制的其他操作。例如,您应该有
CheckUserInput
ExecuteBarcodeTuff
,并且只有当所有
CheckUserInput
s返回true时,才应该运行新的(尚未实现的)
ExecuteBarcodeTuff


将此方法与其他人的答案混合使用,这些答案会执行Linq查询或类似操作,以确保所有结果都是积极匹配的,这将为您带来您想要的结果。

这不是您正在做的吗?对于每个项目,检查用户输入<代码>检查用户输入然后在输入不正确时退出。当前程序中有什么不起作用?
bool erroroccurrent=false=>替换
foreach
w/
while(erroroccurrent==false)
并在某些输入无效时将
erroroccurrent
设置为true?查看后再次检查是否继续。您希望“不运行”程序的哪个部分?因为如果至少有一个条目不正确,它将退出程序,因此您已经拥有该权限。它将立即退出。然而,如果所有4个都通过了“CheckUserInput”,那么您的程序已经结束了,因为这就是“ReadBarcode”中发生的所有事情。。。如果其中任何一个错误,您不想运行什么?在这种特定情况下,我不想从Switch语句中获取控制台输出。@JNW您是否尝试过我的答案?或者使用LINQ:
bool success=barcodes.All(CheckUserInput)
还可以用于在开关中包含“default”值,并将if check code移动到default中,这意味着如果它不匹配任何情况。我需要编程,首先检查4个输入是否为有效输入(条形码或MPN),然后再从switch语句中给出任何控制台输出。我已经试过上面的方法,但是如果4个中只有一个是有效的,我仍然会得到控制台输出。我从你的答案@Felix中得到了缺少的元素。谢谢,那也行。