2013-10-31 118 views
1

我正在處理數組教程。我對陣列的知識是有限的。 我遇到一個錯誤「operator'+ ='不能應用於'double []'類型的操作數,我不知道爲什麼這樣不起作用,直到本教程要求使用數組進行更改時,原始代碼才正常工作。任何想法無法添加到陣列

原始代碼,而數組:?

string inputString; 
     double sales=0, totalA = 0, totalB = 0, totalC=0; 
     double total = 0; 
     string initial; 

     Console.Write("Enter family initial: "); 
     inputString = Console.ReadLine(); 
     initial = inputString.ToString(); 

     while (initial != "z" && initial != "Z") 
     { 
      Console.Write("Enter a sales amount: "); 
      inputString = Console.ReadLine(); 
      sales = Convert.ToDouble(inputString); 
      total += sales; 

      if (initial == "A") 
      { 
       totalA += sales; 
      } 
      else if (initial == "B") 
      { 
       totalB += sales; 
      } 
      else if (initial == "C") 
      { 
       totalC += sales; 
      } 


      Console.Write("Enter family initial: "); 
      inputString = Console.ReadLine(); 
      initial = inputString.ToString(); 

     } 
     Console.WriteLine("Family A earned: {0}", totalA.ToString("C")); 
     Console.WriteLine("Family B earned: {0}", totalB.ToString("C")); 
     Console.WriteLine("Family C earned: {0}", totalC.ToString("C")); 
     Console.WriteLine("Grand Total Sales: {0}",total.ToString("c")); 

這是我試圖修改代碼(每個請求)的縮寫,積累總計銷售值存儲在陣列中的什麼,我可以在任何想法做到這一點嗎?

string inputString; 
// double sales=0, totalA = 0, totalB = 0, totalC=0; 
double sales; 
double total = 0; 
string initial; 

string[] familyNames = {"Anderson","Bowman","Claxton"}; 
string[] inital = { "A", "B", "C" }; 
double[] totalA, totalB, totalC; 

Console.Write("Enter family initial: "); 
inputString = Console.ReadLine(); 
initial = inputString.ToString(); 

while (initial != "z" && initial != "Z") 
{ 
    Console.Write("Enter a sales amount: "); 
    inputString = Console.ReadLine(); 
    sales = Convert.ToDouble(inputString); 
    total += sales; 




    if (initial == "A") 
    { 
     totalA += sales; 
    } 
    else if (initial == "B") 
    { 
     totalB += sales; 
    } 
    else if (initial == "C") 
    { 
     totalC += sales; 
    } 


    Console.Write("Enter family initial: "); 
    inputString = Console.ReadLine(); 
    initial = inputString.ToString(); 

} 
Console.WriteLine("Family A earned: {0}", totalA.ToString("C")); 
Console.WriteLine("Family B earned: {0}", totalB.ToString("C")); 
Console.WriteLine("Family C earned: {0}", totalC.ToString("C")); 
Console.WriteLine("Grand Total Sales: {0}",total.ToString("c")); 
+2

您未使用總計數組。嘗試將其更改爲'double totalA,totalB,totalC;' – LarsTech

+0

您可能不需要數組來計算總數,而不是數組只是取消註釋上面的變量聲明行 – Habib

+0

看起來您甚至不需要數組,而「總」價值可能都是「雙」。要麼是這樣,要麼我真的不明白你想要做什麼。 – crashmstr

回答

1

由於這是一個你正在學習的教程,我認爲這個練習的目的是消除3 total變量並使用數組來代替。

因此,您只需要一個3元素數組:totals,而不是totalA,totalBtotalC。也許是這樣的:

string inputString; 
double sales; 
double total = 0; 
string initial; 

string[] familyNames = {"Anderson","Bowman","Claxton"}; 
string[] inital = { "A", "B", "C" }; 
double[] totals = new double[3]; 

Console.Write("Enter family initial: "); 
inputString = Console.ReadLine(); 
initial = inputString.ToString(); 

while (initial != "z" && initial != "Z") 
{ 
    Console.Write("Enter a sales amount: "); 
    inputString = Console.ReadLine(); 
    sales = Convert.ToDouble(inputString); 
    total += sales; 

    if (initial == "A") 
    { 
     totals[0] += sales; 
    } 
    else if (initial == "B") 
    { 
     totals[1] += sales; 
    } 
    else if (initial == "C") 
    { 
     totals[2] += sales; 
    } 


    Console.Write("Enter family initial: "); 
    inputString = Console.ReadLine(); 
    initial = inputString.ToString(); 

} 
Console.WriteLine("Family A earned: {0}", totals[0].ToString("C")); 
Console.WriteLine("Family B earned: {0}", totals[1].ToString("C")); 
Console.WriteLine("Family C earned: {0}", totals[2].ToString("C")); 
Console.WriteLine("Grand Total Sales: {0}",total.ToString("c")); 

有一些方法可以優化此,例如使用循環打印出來的總數,但我會留給你。

1

以下行有問題

totalA += sales; 
totalB += sales; 
totalC += sales; 

因爲totalA/totalB/totalC被聲明爲雙數組。

分析完你的代碼之後,我覺得你不需要雙數組。 因此,要解決此問題,您需要更改以下

double[] totalA, totalB, totalC; 

double totalA = 0; 
double totalB = 0; 
double totalC = 0; 
0

+操作不適用於數組。 例如:

double[] array = new double[2]; 
array = array + 2; //// this is undefined. 

在你的情況是總量a,共計b,totalC double(值)或double[](陣列)?