【问题标题】:Inputting values for jagged array为锯齿状数组输入值
【发布时间】:2017-09-21 11:45:23
【问题描述】:

好的,我正在尝试编写一个简单的程序,读取给定日期售出的比萨饼数量,然后让用户输入当天售出的比萨饼类型(我需要使用 Split()用户输入)。

我在为锯齿状数组填充列时遇到问题。

现在,我可以得到我必须为仅售出 1 个比萨饼而工作的东西,但除此之外,它不会接受我对当天售出的比萨饼类型的用户输入(列值)。它不会将用户输入作为单独的项目读取,因此一旦我输入,它就会进入下一行,就像它在等待数据而不是继续前进一样。 (因为我测试了一天,一旦用户输入被读入它就会结束程序)。

我不太确定我的问题出在哪里,我的循环将我的列值放入其中,但我认为它与读取用户输入并将其放置在锯齿状数组的列中有关。任何帮助都会很棒。

static void Main(string[] args)
    {
        Greeting();
        string[][] pizza = new string[7][];
        GetPizzas(pizza);

    }

    static void Greeting()
    {
        Write("Welcome to Z's Pizza Report!");
    }

    static void GetPizzas(string[][] array)
    {
        int numOfPizza;
        string day;
        for (int r = 0; r < array.Length; r++)
        {
            if (r == 0)
            {
                day = "Monday";
                Write("How many total pizzas were there for {0}? ", day);
                numOfPizza = int.Parse(ReadLine());
                while (numOfPizza < 0)
                {
                    Write("Number cannot be negative. Try Again: ");
                    numOfPizza = int.Parse(ReadLine());
                }

                array[r] = new string[numOfPizza];
                Write("Enter all the pizzas for {0}, seperated by spaces: ", day);

                for (int c = 0; c < array[r].Length; c++)
                {
                    string total = ReadLine();
                    array[c] = total.Split(' ');
                    while (array[r] != array[c])
                    {
                        Write("Input does not match number needed. Try Again: ");
                        total = ReadLine();
                        array[c] = total.Split(' ');
                    }
                }
            }
            else if (r == 1)
            {
                day = "Tuesday";
            }
            else if (r == 2)
            {
                day = "Wednesday";
            }
            else if (r == 3)
            {
                day = "Thursday";
            }
            else if (r == 4)
            {
                day = "Friday";
            }
            else if (r == 5)
            {
                day = "Saturday";
            }
            else
            {
                day = "Sunday";
            }

        }
    }

【问题讨论】:

  • 什么是Write
  • 我使用的是using static System.Console;,所以我不必一直使用Console.
  • 因此,您在 array[r] 上执行 for 循环有点奇怪,每次迭代都执行 ReadLine(),因为您一次要求所有的比萨饼,分开按空格。
  • 您正在处理锯齿状数组[][] 并且在初始化期间您使用数组[]?
  • @MikeMcCaughan 是的,我正在尝试填充锯齿状数组的列,这不正确吗?

标签: c# arrays split jagged-arrays


【解决方案1】:

代码对我来说似乎过于复杂,但这样的事情有帮助吗?哦,请注意我正在使用内置的DayOfWeek 枚举来解析当天的友好名称。这和你的区别在于Sunday 是一天0

这行代码将整数转换为枚举,然后获取字符串值:

var dayOfWeek = ((DayOfWeek)i).ToString();

我还添加了一个辅助函数来从用户那里获取一个整数。它接受一个提示字符串、一个错误字符串、一个可选的最小值和一个可选的最大值,然后提示用户输入一个整数,并且在用户输入有效值之前不会返回:

static int GetIntFromUser(string prompt, string error, 
    int minValue = int.MinValue, int maxValue = int.MaxValue)
{
    int result;

    if (!string.IsNullOrEmpty(prompt)) Console.Write(prompt);

    while (!int.TryParse(Console.ReadLine(), out result) 
           || result < minValue || result > maxValue)
    {
        if (!string.IsNullOrEmpty(error)) Console.Write(error);
    }

    return result;
}

其余代码如下:

static void Main(string[] args)
{
    var pizzasSold = new string[7][];

    GetPizzas(pizzasSold);

    for (int i = 0; i < pizzasSold.Length; i++)
    {
        var dayOfWeek = ((DayOfWeek)i).ToString();
        Console.WriteLine("You sold {0} pizzas on {1}: {2}", 
            pizzasSold[i].Length, dayOfWeek, string.Join(", ", pizzasSold[i]));
    }

    Console.Write("\nDone!\nPress any key to exit...");
    Console.ReadKey();
}

static void GetPizzas(string[][] array)
{
    for (int r = 0; r < array.Length; r++)
    {
        var dayOfWeek = ((DayOfWeek)r).ToString();

        var numPizzasSold =
                GetIntFromUser($"How many total pizzas were there for {dayOfWeek}? ",
                "Number cannot be negative. Try Again: ", 0);

        Console.Write($"Enter all the pizzas for {dayOfWeek}, seperated by spaces: ");
        var pizzasSold = Console.ReadLine().Split(new[] { ' ' }, 
            StringSplitOptions.RemoveEmptyEntries);

        while (pizzasSold.Length != numPizzasSold)
        {
            Console.Write($"Input does not match number needed. Try Again: ");
            pizzasSold = Console.ReadLine().Split(new[] { ' ' }, 
                StringSplitOptions.RemoveEmptyEntries);
        }

        array[r] = pizzasSold;
    }
}

【讨论】:

  • 很遗憾没有。必须对其进行格式化,以便询问当天售出了多少,然后根据该数字,然后询问当天售出的比萨饼类型。如果有意义的话,将会有一个检查来验证输入的比萨饼类型是否与当天售出的比萨饼数量相匹配。我完全意识到它过于复杂,不幸的是它必须是那样的。
  • 很奇怪。好的,根据该要求更新了答案(并添加了一个从用户那里获取 int 的辅助函数)
【解决方案2】:
using System;

namespace ConsoleApp12
{
    class Program
    {
        static void Main(string[] args)
        {
            int[][] n = new int[3][];
            int i;
            n[0] = new int[4];
            n[1] = new int[3];
            n[2] = new int[2];
            // n[1] = new int[] { 1, 2, 3 };
            // Console.WriteLine("enter the rollno");
            for (i = 0; i < 4; i++)
            {

                n[0][i] = Convert.ToInt32(Console.ReadLine());


            }
            for (i = 0; i < 3; i++)
            {

                n[1][i] = Convert.ToInt32(Console.ReadLine());


            }
            for (i = 0; i < 2; i++)
            {

                n[2][i] = Convert.ToInt32(Console.ReadLine());


            }


            //   for (i = 0; i < 3; i++)
            // {
            //   n[i][j] = Convert.ToInt32(Console.ReadLine());
            //}

            for (i = 0; i <4; i++)
            
                              {
                    Console.Write(n[0][i] + " ");
                }
            Console.WriteLine();
           for (i = 0; i < 3; i++)
            
                    {
                        Console.Write(n[1][i] + " ");
                    }

                

            }
    }
}

        

    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-02
    • 1970-01-01
    • 2020-10-21
    • 2015-09-23
    • 1970-01-01
    • 2013-06-21
    • 2015-07-01
    相关资源
    最近更新 更多