如何在控制台应用程序中只将数字存储在字符串数组中

时间:2017-03-20 07:59:46

标签: c#

所以,我正在尝试为我的学校项目创建一个披萨订购计划,该计划要求客户/用户输入他们的详细信息,例如姓名电话号码地址和不是。我将所有这些信息存储到字符串数组中,但是如何只允许将数字输入到电话号码部分。我尝试使用我提供的一种方法,但它似乎并没有使用它只是错误。

这是我目前的代码

public static string[] GetCustInfo(string [] custInfo)
    {
        start:
        bool success = false;

        Console.Write("\n" + "Please input D for Delivery OR P for Pickup($3 for Delivery): "  + "\n");
        custInfo[0] = Console.ReadLine();

        while (success != true) 
        {

            if (custInfo[0] == "D" || custInfo[0] == "d")
            {

                custInfo[1] = ReadString("Please enter your name: ");
                Console.Write(Readint("Please enter your Ph No. : "));
                custInfo[2] = Console.ReadLine();
                custInfo[3] = ReadString("Please enter your adress: ");


                success = true;
            }
            else if (custInfo[0] == "P" || custInfo[0] == "p")
            {
                custInfo[1] = ReadString("Please enter your name: ");

                success = true;
            }
            else
            {
                goto start;
            }

        }

        return custInfo;
    }

以下是我用来阻止用户输入数字或字母的方法:

 public static int Readint(String prompt)
    {
        int userInput = 0;
        Boolean success = false;
        while (!success)
        {
            Console.Write(prompt);
            try
            {
                userInput = Convert.ToInt32(Console.ReadLine());
                success = true;
            }
            catch 
            {
                Console.WriteLine("Please Enter a VALID NUMBER");
            }
        }
        return userInput;
    }

 public static string ReadString(String prompt)
    {
        string userInput = " ";
        Boolean success = false;
        while (!success)
        {
            Console.Write(prompt);
            try
            {
                userInput = Console.ReadLine();
                if (userInput.Length <= 20 && userInput.Length>1)
                {

                    success = true;

                    foreach (char charcter in userInput)
                    {
                        if (Char.IsNumber(charcter) || Char.IsSymbol(charcter) || Char.IsPunctuation(charcter))
                        {
                            success = false;
                        }
                    }
                }
                if (success == false)
                {
                    Console.WriteLine("Please enter a valid input!" + "\n");
                }
                else
                {
                    success = true;
                }
            }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        return userInput;
    }

我尝试插入:

custInfo[2] = Readint("Please enter your Ph No.");

但它只是给了我一个错误说:

  

&#34;不能将int类型转换为字符串&#34;

3 个答案:

答案 0 :(得分:1)

  

但我如何只允许将号码输入电话号码

ReadInt方法已经使用while-loop和try / catch子句来处理这一点。如果要将此信息存储到数组中,则需要将数字输入到数组的正确/拟合数据类型中:string。您返回ReadInt的值为int,此类具有ToString方法的实现,在您的情况下,您将int转换为string。因此,您只需在ReadInt的返回值上调用它:

custInfo[1] = ReadString("Please enter your name: ");
custInfo[2] = Readint("Please enter your Ph No.").ToString(); 
custInfo[3] = ReadString("Please enter your adress: ");

答案 1 :(得分:0)

Insted of public static string[] GetCustInfo(string [] custInfo),您可以添加对象数组,但请记住下一步:对象数组在单个集合中存储不同类型的元素。对象引用可以指向任何派生类型实例。

无论你能做什么:

object[] array1 = new object[5];
array1[0] = new object();
     array1[1] = new StringBuilder("Initialized");
     array1[2] = "String literal";
     array1[3] = 3;
     array1[4] = null;

所以在你的情况下:这可能看起来像这样

public static object[] GetCustInfo(object [] custInfo)

答案 2 :(得分:0)

您尝试将int值插入到字符串数组中。确实有意义将值转换为字符串吗?