gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:13:08 24 4
gpt4 key购买 nike

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

程序无一异常(exception)地编译和运行。但如果有一个输入错误,其他三个输入仍然会通过。
虽然,我真正需要的是在触发该方法之前确保 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);
}
}
}
}

最佳答案

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

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;
}

}

ReadBarcodes 可能看起来像这样:

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
}

关于c# - 在触发方法 c# 之前需要验证一组用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43761718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com