我正在编写数组教程。我对数组的了解有限。我遇到错误“运算符'+ ='不能应用于'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"));