C# の初心者 (これまでのところ 1 週間のコーディングのみ) で、練習用プログラムを作成しようとしています。「price1」と「price2」に保存したいデータを取得できないようです。エラーは CS0165 割り当てられていないローカル変数 'price1' および 'price2' の使用です。
コード行を移動して return コマンドを追加しようとしましたが、よくわかりません。
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」に格納されたデータを取得しようとしているので、最後にそれらを追加できます。ここで用語が間違っている場合は申し訳ありません。