2013-10-19 4 views
1

이것을 시도 할 때마다 벽돌 벽을 돌고 있습니다. 내가 알아 내지 못한 것은 내 코드가 내 정보를 텍스트 파일에서 가져 와서 정렬하지 않는 이유입니다. 내가 한 모든 일은 좋게 보입니다. 76, 86 및 103 행에서 3 개의 오류가 발생합니다. 'yourChoicesItems'은 현재 컨텍스트에 존재하지 않으며 yourChoicesPrices '는 현재 컨텍스트에 존재합니다. 하지만 나는 무엇이 잘못되었는지 보지 못합니다. 다음과 같은 날짜를 넣을 때 : public string [] yourChooseItems = {etc ....} 작동합니다. 왜이 기능이 작동하지 않습니까?파일에서 텍스트를 사용하여 정렬하려고 시도합니다.

내 텍스트 파일은 다음과 같습니다

BlueberryBagels 0.75
해시 브라운 (A 각 줄 앞에 공간이) 2.50
BottledSoda 1.50
커피 0.90
도넛 1.50
감자 튀김 1.50
BlueberryMuffins 0.85
LiteYogurt 0.75
HotChocolate 1.75
Onio nSoup 3.00
PecanPie 2.75
PurpleYam 2.75
StrawberryBagels 0.80
토스트 2.00
VanillaIceCream 2.75
IcedTea 1.00

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; 
    } 
} 
+2

일반적인 지침으로, 주어진 문제에 대해 너무 많은 코드입니다. 읽을 수 있도록 노력하십시오. –

+0

휠을 재발 명하지 마십시오. 배열을 정렬하는 것은 해결 된 문제입니다. 정렬 알고리즘을 직접 작성하지 않고 .NET에서 여러 가지 방법으로이를 수행 할 수 있습니다. 예를 들어'Array.Sort' 만 사용할 수 있습니다. –

+0

@ThomasLevesque - 이것은 학습 운동의 모든 흔적을 가지고 있으며, 일반적으로 뭔가를 재창조하는 것에 관한 것입니다. –

답변

1

귀하의 코드가 같은 자바이며이 C#에서 내가 만든 너무 정확한 아니에요 일부 수정 :

class FoodData 
{ 
    public string FoodName { get; set; } 
    public double Price { get; set; } 
} 

전나무 대신 하나의 String

if (!System.IO.File.Exists("menu.txt")) 
      return; 
     string[] values; 
     double price; 
     List<FoodData> lines = new List<FoodData>(); 
     using (System.IO.StreamReader sr = new System.IO.StreamReader("menu.txt")) 
     { 
      while (sr.Peek() > -1) 
      { 
       values = sr.ReadLine().Split(' '); 
       FoodData tmp = new FoodData(); 
       tmp.FoodName = values[0]; 
       tmp.Price = Convert.ToDouble(values[1]); 
       lines.Add(tmp); 
      } 
     } 

이 파일을 읽을 C#에서와 일반적인 방법입니다의 두 가지 속성을 가진 클래스를보다 효율적으로 사용을 그래서 일 모두의 우리는 OOP 언어를 사용하고 있습니다. sr이 닫히지 않도록 포인터를 즉시 처리하기 때문에 permitt를 사용합니다. 당신이 목록

lass FoodData_SortByPriceByAscendingOrder : IComparer<FoodData> 
{ 
    public int Compare(FoodData x, FoodData y) 
    { 
     if (x.Price > y.Price) return 1; 
     else if (x.Price < y.Price) return -1; 
     else return 0; 
    } 
} 
class FoodData_SortByPriceByDescendingOrder : IComparer<FoodData> 
{ 
    public int Compare(FoodData x, FoodData y) 
    { 
     if (x.Price < y.Price) return 1; 
     else if (x.Price > y.Price) return -1; 
     else return 0; 
    } 
} 
class FoodData_SortByName : IComparer<FoodData> 
{ 
    public int Compare(FoodData x, FoodData y) 
    { 
     return string.Compare(x.FoodName, y.FoodName); 
    } 
} 

이 3 개 클래스 (A FoodData 목록을받을)를 IComparer 인터페이스를 상속의 Sort 방법을 사용하려는 경우

은 이제에서 IComparable 인터페이스를 구현해야합니다. 그것은 이해하기 어려운에 아니지만 클래스 완료 후에는 예를 들어

을 참조를 찾아 herehere 당신은 같은 주에서 이러한 전화 :

FoodData_SortByPriceByAscendingOrder fAsc = new FoodData_SortByPriceByAscendingOrder(); 
     lines.Sort(fAsc); 
     FoodData_SortByPriceByDescendingOrder fDesc = new FoodData_SortByPriceByDescendingOrder(); 
     lines.Sort(fDesc); 
     FoodData_SortByName fByName = new FoodData_SortByName(); 
     lines.Sort(fByName); 
0

yourChoicesPricesyourChoicesItems가 선언되지 않습니다 주어진 코드. 그래서이 오류가 발생합니다. 당신은 그들을 선언해야합니다.

관련 문제