为什么它向我显示了一个错误,即成本是一个未分配的变量
本文关键字:一个 分配 变量 显示 错误 为什么 | 更新日期: 2023-09-27 18:19:40
Console.WriteLine("enter the sirial number of the followin bevrege you want to buy");
Console.WriteLine("beverge cost");
Console.WriteLine("1.ness cafe 2.25");
Console.WriteLine("2.black coffe 2.25");
Console.WriteLine("3.tea 1.30");
Console.WriteLine("4.hot choclate 2.50");
Console.WriteLine("5.soup 3.10");
Console.WriteLine("6.coca cola 3.30");
Console.WriteLine("7.orange juice 3.20");
int sirial;
sirial = int.Parse(Console.ReadLine());
double mouny, cost;
Console.WriteLine("enter how much ouny you enterd to the machine ");
mouny = double.Parse(Console.ReadLine());
if (sirial == 1 || sirial == 2)
cost = 2.25;
if (sirial == 3)
cost = 1.30;
if (sirial == 4)
cost = 2.50;
if (sirial == 5)
cost = 3.10;
if (sirial == 6)
cost = 3.30;
if (sirial == 7)
cost = 3.20;
if (mouny == cost)
Console.WriteLine(" n/ thank you for buying");
如果用户输入的数字不是1到7,则不会为cost
分配值。您可以给它一个初始值0来消除编译器警告,但随后我会添加一个验证,验证用户是否输入了有效的选项。