C# 新手(到目前为止只编码一周)试图创建一个练习程序。似乎无法获得我想要存储在“price1”和“price2”中的数据。错误是 CS0165 使用未分配的局部变量“price1”和“price2”。
我试过移动代码行并添加一个返回命令,但我似乎不太明白。
Console.Write("What grocery are you buying: ");
string product1 = Console.ReadLine();
Console.Write("How many are you buying: ");
int quantity1 = Convert.ToInt32(Console.ReadLine());
double price1;
if (product1 == "Steak")
{
price1 = Convert.ToDouble(steak.price * quantity1);
}
if (product1 == "Cheerios")
{
price1 = Convert.ToDouble(cheerios.price * quantity1);
}
if (product1 == "Pepsi")
{
price1 = Convert.ToDouble(pepsi.price * quantity1);
}
if (product1 == "Celeste Pizza")
{
price1 = Convert.ToDouble(celeste.price * quantity1);
}
Console.Write("What second grocery are you buying: ");
string product2 = Console.ReadLine();
Console.Write("How many are you buying: ");
int quantity2 = Convert.ToInt32(Console.ReadLine());
double price2;
if (product2 == "Steak")
{
price2 = Convert.ToDouble(steak.price * quantity2);
}
if (product1 == "Cheerios")
{
price2 = Convert.ToDouble(cheerios.price * quantity2);
}
if (product1 == "Pepsi")
{
price2 = Convert.ToDouble(pepsi.price * quantity2);
}
if (product1 == "Celeste Pizza")
{
price2 = Convert.ToDouble(celeste.price * quantity2);
}
Console.WriteLine(price1 + price2);
试图获取存储在“price1”和“price2”中的数据,以便我可以在最后将它们加在一起。对不起,如果我在这里弄错了任何术语。