【发布时间】: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