【问题标题】:How to store 4 digit PINS in the Array[10] and then extract them如何在 Array[10] 中存储 4 位 PIN 然后提取它们
【发布时间】:2020-08-15 17:32:17
【问题描述】:

选项 1 添加 PINS ,选项 4 显示它们(2 和 3 未完成),如何在 GetValidInt 方法中修改此代码以将 myInt 存储为字符串?

static void PopulateArray(int[] theArray)
{
    for (int i = 0; i < theArray.Length; i++)
    {
        theArray[i] = GetValidInt($"Please enter 4-Digit PIN or q to exit #{i + 1}: ", 0, 9999);
    }
}

static int GetValidInt(string prompt, int min, int max)
{
    bool valid = false;
    int myInt = -1;
    //string myInt; //trying to convert a int to string
    do
    {
        Console.Write(prompt);
        try
        {
            //myInt = Console.ReadLine();
            myInt = int.Parse(Console.ReadLine());
            if (myInt < min || myInt > max)
            {
                throw new Exception("Provided integer was outside of the bounds specified.");
            }
            valid = true;
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Parse failed: {ex.Message}");
        }
    } while (!valid);
    //enter code here
    return myInt;
}

我想首先检查用户是否输入了 0 到 9999 之间的数字,并且数据可以有前导“0”,因为这些是 PIN 码(例如:“0001”或“0123”)。然后我将它们存储在 [10] 的数组中,稍后根据用户请求检索它们。这就是为什么我首先使用 int 格式来检查 MIN 和 MAX,然后我需要将其转换为字符串进行存储,这样我就不会丢失“零”。我可以将我的范围从 999 限制到 10000,但是我将无法存储像“0001”或“0123”这样的引脚,因为它会将其存储为 1 和 123。

【问题讨论】:

  • 为什么要将myInt存储为字符串,作为返回int的方法?

标签: c# arrays string


【解决方案1】:

主要问题是您的代码不遵循操作的自然过程。这是我的建议(利用System.Text.RegularExpressions 命名空间):

static Regex PinRegex = new Regex(@"\d{4}");

static bool isPin(string s) => PinRegex.IsMatch(s);

static bool isExit(string s) => s == "q";

static void PopulateArray(string[] theArray)
{
    for (int i = 0; i < theArray.Length; i++)
    {
        Console.Write($"Please enter 4-Digit PIN or q to exit #{i + 1}: ");
        string userAnswer = Console.ReadLine();
        if (isExit(userAnswer))
            exit();  // To be implemented
        else if (isPin(userAnswer))
            theArray[i] = userAnswer;
        else
            unrecognizedAnswer(); // To be implemented
    }
}

【讨论】:

    【解决方案2】:

    简单,你想要一个字符串,你可以做两件事,我会给你一些选择:

    第一个选项,参数始终是字符串:

    static string GetValidInt(string prompt, int min, int max)
    {
        bool valid = false;
        string myInt = "-1";
        //string myInt; //trying to convert a int to string
    do
    {
    Console.Write(prompt);
    try
    {
    //myInt = Console.ReadLine();
    myInt = Console.ReadLine(); 
      if (int.Parse(myInt)< min || int.TryParse(myInt) > max)
        {
        throw new Exception("Provided integer was outside of the bounds specified.");
        }   
    valid = true;
    }
    catch (Exception ex)
      {
        Console.WriteLine($"Parse failed: {ex.Message}");
        }
    } while (!valid);
        enter code here
    return myInt;
    }
    

    第二个选项是意识到为什么你需要 myInt 是一个字符串,我相信你正在尝试传递你正在阅读的字符串,在这种情况下,我会以不同的方式编写代码,我可以向你展示代码,但我认为这超出了问题的范围。如果这就是您所追求的,请查看https://docs.microsoft.com/en-us/dotnet/api/system.console.readline?view=netcore-3.1

    【讨论】:

    • 我想首先检查用户是否输入了 0 到 9999 之间的数字,并且数据可以有前导“0”,因为这些是 PIN 码(例如:“0001”或“0123”)。然后我将它们存储在 [10] 的数组中,稍后根据用户请求检索它们。这就是为什么我首先使用 int 格式来检查 MIN 和 MAX,然后我需要将其转换为字符串进行存储,这样我就不会丢失“零”。我可以将我的范围从 999 限制到 10000,但是我将无法存储像“0001”或“0123”这样的引脚,因为它会将其存储为 1 和 123
    • 1.- 要检查用户是否输入了一个数字,您仍然可以将其存储在一个字符串中,然后检查它是否是一个数字并解析它 (stackoverflow.com/questions/894263/…)。 2.-这意味着该数组是字符串数组还是整数数组?你想要什么,你可以做任何!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-25
    • 2010-10-30
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 1970-01-01
    相关资源
    最近更新 更多