我正在處理數組教程。我對陣列的知識是有限的。 我遇到一個錯誤「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"));
您未使用總計數組。嘗試將其更改爲'double totalA,totalB,totalC;' – LarsTech
您可能不需要數組來計算總數,而不是數組只是取消註釋上面的變量聲明行 – Habib
看起來您甚至不需要數組,而「總」價值可能都是「雙」。要麼是這樣,要麼我真的不明白你想要做什麼。 – crashmstr