c# - 为什么在调试时我的代码中会出现 System.String [ ]?
问题描述
我正在为初学者准备 c# 的最后一个项目,但我似乎无法让我的列表正常工作。它也会在“填充”订单后自动关闭。
class Burgare
{
private string[] hamburger = new string[24];
private int no_burger = 0;
public void add_burger()//Ordering of burgers
{
Console.Clear(); //Clears console to make it look cleaner
while (true)
{
int Burger_option = 0;
do
{
Console.WriteLine("");// The options user can choose from
Console.WriteLine("Please choose burgers from our menu:");
Console.WriteLine("-------Burgers--------");
Console.WriteLine("1 Original One 109");
Console.WriteLine("2 Pig & Cow 109");
Console.WriteLine("3 Spice & Nice 109");
Console.WriteLine("4 Green One 109");
Console.WriteLine("0 Go back to main menu");
Console.WriteLine("----------------------");
Console.WriteLine("");
try //Making sure the user only picks what is on the menu
{
Burger_option = int.Parse(Console.ReadLine());
}
catch
{
Console.WriteLine("You can only choose what is on the menu!");
}
switch (Burger_option) //Depending on the number user presses on keyboard different burgers will be added to order
{
case 1:
Console.WriteLine("You've added the Original One to your order");
Console.WriteLine("If you're done press 0 to go back to the main menu");
hamburger[no_burger] = "Original One";
break;
case 2:
Console.WriteLine("You've added the Pig & Cow to your order");
Console.WriteLine("If you're done press 0 to go back to the main menu");
hamburger[no_burger] = "Pig & Cow";
break;
case 3:
Console.WriteLine("You've added the Spice & Nice to your order");
Console.WriteLine("If you're done press 0 to go back to the main menu");
hamburger[no_burger] = "Spice & Nice";
break;
case 4:
Console.WriteLine("You've added the Green One to your order");
Console.WriteLine("If you're done press 0 to go back to the main menu");
hamburger[no_burger] = "Green One";
break;
case 0: //Sends user back to main menu
Run();
break;
}
no_burger++; //Adds burger
} while (no_burger != 0);
if (no_burger <= 25) //Maximum number of orders
{
Console.WriteLine("The restaurant can't take more than 24 orders of burgers at the time");
Run(); //Sends user back to main menu when the order is over 24
}
}
}
public void print_order()
{
Console.WriteLine("-Your current order-");
foreach (var burger in hamburger) //Showing a list of what is in the order
{
Console.WriteLine(hamburger);
if (burger != null)
Console.WriteLine(burger);
else
Console.WriteLine("Empty space"); //Making empty space to show where you can add more burgers
}
}
输入 24 个订单后,我收到错误“System.IndexOutOfRangeException:'索引超出了数组的范围。'”我环顾四周,但仍然不明白如何修复我的代码。我希望它在告诉用户订单已满后返回主菜单。
对于我的第二个问题 System.String [] 问题,它出现在我输入“print_order”时。它显示为 System.String [] Empty space System.String [] Empty space 等等。如果可能的话,我想要么完全删除它,要么至少用 1,2,3...等替换它。
解决方案
首先,请每个问题只发布一个问题;你在这里描述了很多问题,但实际上只问了一个问题。如果您有多个问题,请发布多个问题。
为什么我
System.String[]
在调试时进入我的代码?
默认情况下,C# 会在您打印出类型时给出类型的名称,并且您正在打印一个字符串数组。要将字符串数组转换为字符串,请使用类型的Join
方法string
。注意不要将它与Join
序列的扩展方法混淆。
它也会在“填充”订单后自动关闭。
当控制台程序的控制到达末尾时Main
,它会终止程序。如果您希望发生其他事情,请编写指示您希望发生的事情的代码。
输入 24 个订单后,我收到错误“System.IndexOutOfRangeException:'索引超出了数组的范围。'”我环顾四周,但仍然不明白如何修复我的代码。我希望它在告诉用户订单已满后返回主菜单。
您已为 24 个事物保留空间,并且您已尝试访问第 25 个事物。这是一个致命的错误。不要那样做。您需要检测并防止这种情况。
如果您希望它在告诉用户订单已满后返回主菜单,请编写代码来执行此操作。如何?由于显然主菜单可能会多次出现,因此您需要将其置于循环中。您已经编写了两个循环;在循环中放更多的东西!
在我们查看您的代码时,有一些更好的建议:
- 现在开始使用 C# 语言的约定。你写了
Console.WriteLine
然后做了一个方法print_order
。 遵循框架设计者设定的模式,而不是你自己编造的东西。类型和方法应该是CasedLikeThis
. - 永远不要把
int.Parse
a 放进去try-catch
,永远。这int.TryParse
就是为了! - 你的程序有错误;如果有人输入,比如 7,会发生什么?处理那个案子!
推荐阅读
- pandas - 为什么我在有或没有列选择的情况下使用 pandas groupby() 获得不同的组大小数?
- group-by - “错误:n() 只能在数据上下文中调用”
- javascript - 如何在使用节点 js 调用 API 时获得准确的响应状态码?
- android - 如何仅使用 simpledateformat 更改包含月份和年份的字符串
- html - 如何修复页面以在谷歌缓存中正确显示
- android - 我正在尝试让代码工作以解码并显示原始 H264 tcp/ip 视频流。有人可以诊断出什么问题吗?
- c# - 以下使用 ResidentArrayGeneric 的 Hybridizer 代码有什么问题?
- here-api - 此处地图:在同一页面上的不同地图上加载多个 KML 文件并在每个地图上启用事件
- jquery - 如何在 JQuery 中使用 $.get 修复类型错误(不是函数)
- linux - 读取输入文件时出错:密钥已过期