Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在控制台应用程序中仅将数字存储在字符串数组中_C# - Fatal编程技术网

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

C# 如何在控制台应用程序中仅将数字存储在字符串数组中,c#,C#,因此,我正在尝试为我的学校项目创建一个比萨饼订购计划,该计划要求客户/用户输入他们的详细信息,例如姓名、电话号码、地址等等。我将所有这些信息存储到一个字符串数组中,但如何只允许在电话号码部分输入号码。我试着使用一种方法,我已经提供了,但它似乎不工作,它只是错误 这是我目前的代码 public static string[] GetCustInfo(string [] custInfo) { start: bool success = false;

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

这是我目前的代码

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;
    }
但它只是给了我一个错误,说:

“无法将类型int暗示为字符串”


除了
公共静态字符串[]GetCustInfo(字符串[]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)
但是如何只允许在电话号码中输入号码

您已经使用while循环和try/catch子句处理了这一点。如果要将此信息存储到数组中,则需要将数字输入到数组的正确/适合的数据类型中,并说:
string
。您返回的
ReadInt
值是一个
int
,这个类有一个方法的实现,在您的例子中,该方法将
int
转换为
字符串。因此,您只需在
ReadInt
的返回值上调用它:

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

你试图在字符串数组中插入一个int值。当然,你有必要把这个值转换成一个字符串。

不是你的问题的答案,但是考虑用一个略微重构的环替换“Goto”语句,你的比萨饼最好是强制性的3美元的交付费用。大多数免费送货。:)这是学校作业的要求,不是我的想法,我只是在编写代码。试一试会比下一步更好吗?它不是循环返回,而是阻止用户继续
custInfo[1] = ReadString("Please enter your name: ");
custInfo[2] = Readint("Please enter your Ph No.").ToString(); 
custInfo[3] = ReadString("Please enter your adress: ");