每次我尝试这样做时,我都会遇到一堵砖墙。我无法弄清楚为什么我的代码不会从文本文件中提取我的信息并对其进行排序。我所做的一切看起来都很好。我在第 76、86 和 103 行收到 3 个错误。它们声明“yourChoicesItems”在当前上下文中不存在,并且 yourChoicesPrices' 在当前上下文中不存在。但我看不出有什么问题。当我输入日期时,例如: public string[] yourChoicesItems = {etc....} 它可以工作。为什么这不起作用?
我的文本文件是:(每行前有一个空格)
BlueberryBagels 0.75
HashBrowns 2.50
BottledSoda 1.50
Coffee 0.90
Donut 1.50 French Fries
1.50
BlueberryMuffins 0.85
LiteYogurt 0.75
HotChocolate 1.75
OnionSoup 3.00
PecanPie 2.75
PurpleYam 2.75
StrawberryBagels 0.80
Toast 2.50 Vanced
VanTea
Crece.2.2
using System.IO;
namespace testce
{
public partial class MainScreen : Form
{
static void InsertSort(IComparable[] array)
{
int i, j;
for (i = 1; i < array.Length; i++)
{
IComparable value = array[i];
j = i - 1;
while ((j >= 0) && (array[j].CompareTo(value) > 0))
{
array[j + 1] = array[j];
j--;
}
array[j + 1] = value;
}
}
int count = 0;
double totalTax = 0;
double totalSale = 0;
public MainScreen()
{
FileStream fStream = new FileStream("menu.txt", FileMode.Open, FileAccess.Read);
StreamReader inFile = new StreamReader(fStream);
string inValue;
string[] values;
double price;
List<string> lines = new List<string>();
while (!inFile.EndOfStream)
{
inValue = inFile.ReadLine();
lines.Add(inValue);
values = (inValue.Split(" ".ToCharArray()));
price = double.Parse(values[2]);
InsertSort(values);
}
inFile.Close();
InitializeComponent();
InitializeControls();
for (int index = 0; index < listBox.SelectedIndices.Count; index++)
{
subTotal = subTotal + yourChoicesPrices[listBox.SelectedIndices[index]];
}
for (int index = 0; index < listBox.SelectedIndices.Count; index++)
{
textBox.AppendText(yourChoicesItems[listBox.SelectedIndices[index]] + "\n");
}
Text = "Thank you for using Food Systems Inc.";
this.listBox.DataSource = yourChoicesItems;
this.btnOne.Text = "Place Order";
this.label.Text = "Menu Selection";
this.labell.Text = "Order Information";
}
public System.Windows.Forms.ListBox listBox;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Button btnOne;
private System.Windows.Forms.TextBox textBox;
}
}