C# 输入锯齿数组的值
好的,我正在尝试制作一个简单的程序,读取给定一天售出的比萨饼数量,然后让用户输入当天售出的比萨饼类型(有了这个,我需要使用Split()和用户输入) 我在填充锯齿数组的列时遇到问题 现在,我只需要卖一个比萨饼就可以得到我所需要的东西,但除此之外,它不接受我的用户输入,即当天出售的比萨饼类型(列值)。它没有将用户输入作为单独的项目读取,所以一旦我输入,它就会转到下一行,就像它在等待数据,而不是继续。(因为我测试了一天,一旦用户输入被读入,程序就会结束) 我不太确定我的问题在哪里,我的循环把我的列值放进去了,但我认为它与读取用户输入并把它放在锯齿数组的列中有关。任何帮助都会很好C# 输入锯齿数组的值,c#,arrays,split,jagged-arrays,C#,Arrays,Split,Jagged Arrays,好的,我正在尝试制作一个简单的程序,读取给定一天售出的比萨饼数量,然后让用户输入当天售出的比萨饼类型(有了这个,我需要使用Split()和用户输入) 我在填充锯齿数组的列时遇到问题 现在,我只需要卖一个比萨饼就可以得到我所需要的东西,但除此之外,它不接受我的用户输入,即当天出售的比萨饼类型(列值)。它没有将用户输入作为单独的项目读取,所以一旦我输入,它就会转到下一行,就像它在等待数据,而不是继续。(因为我测试了一天,一旦用户输入被读入,程序就会结束) 我不太确定我的问题在哪里,我的循环把我的列值
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";
}
}
}
static void Main(字符串[]args)
{
问候语();
字符串[][]pizza=新字符串[7][];
披萨;
}
静态无效问候语()
{
写下(“欢迎来到Z的比萨饼报告!”);
}
静态void GetPizzas(字符串[][]数组)
{
国际努莫夫比萨饼;
弦日;
for(int r=0;r
对我来说,代码似乎过于复杂,但这样做是否有帮助?哦,请注意,我正在使用内置的DayOfWeek
enum来解析当天的友好名称。这与您的区别在于星期日
是一天0
这行代码将整数强制转换为枚举,然后获取字符串值:
var dayOfWeek = ((DayOfWeek)i).ToString();
我还添加了一个helper函数来从用户那里获取一个整数。它接收一个提示字符串、一个错误字符串、一个可选的最小值和一个可选的最大值,然后提示用户输入一个整数,直到用户输入一个有效值后才会返回:
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 int GetIntFromUser(字符串提示,字符串错误,
int minValue=int.minValue,int maxValue=int.maxValue)
{
int结果;
如果(!string.IsNullOrEmpty(prompt))控制台.Write(prompt);
而(!int.TryParse(Console.ReadLine(),out result)
||结果<最小值|结果>最大值)
{
如果(!string.IsNullOrEmpty(错误))Console.Write(错误);
}
返回结果;
}
代码的其余部分如下所示:
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;
}
}
static void Main(字符串[]args)
{
var pizzasSold=新字符串[7][];
GetPizzas(pizzasSold);
for(int i=0;i
使用系统;
名称空间控制台AP12
{
班级计划
{
静态void Main(字符串[]参数)
{
int[][]n=新的int[3][];
int i;
n[0]=新整数[4];
n[1]=新整数[3];
n[2]=新整数[2];
//n[1]=新的int[]{1,2,3};
//Console.WriteLine(“输入卷号”);
对于(i=0;i<4;i++)
{
n[0][i]=Convert.ToInt32(Console.ReadLine());
}
对于(i=0;i<3;i++)
{
n[1][i]=Convert.ToInt32(Console.ReadLine());
}
对于(i=0;i<2;i++)
{
n[2][i]=转换为32(
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] + " ");
}
}
}
}