Выдаёт ArgumentOutOfRangeException вместо возвращаемого числа
Я новичок, прошу тапками не кидаться. проблемка возникла, что после вопроса о названии продукта вместо того , чтобы вернуть цену из каталога , он выдает default. Cовсем не знаю как это исправить. Надеюсь, что там нигде не опечаталась и заранее спасибо
public struct Goods
{
public string name;
public int cost;
public int amount;
}
public static int Katalog(ref Goods[] list, ref int MAX, ref int i)
{
switch( list[i].name)
{
case “milk”: return 10;
case “cola”: return 7;
// Дальше перечисление
default: throw new ArgumetOutOfRangeException()
}
}
public static int Shop( ref Goods[] list)
{……
int MAX;
Console.WriteLine("сколько продуктов хотите купить?");
MAX=int.Parse(Console.ReadLine());
for (int i =1;i<=MAX;i++)
{
try `
{
Console.WriteLine("напиши название продукта",i);
list[i].name=Console.ReadLine();
list[i].name=list[i].name.ToLower();
Goods [] listofGoods= new Goods[20];
list[i].cost= Katalog(ref listofGoods, ref MAX, ref i);
}
catch(ArgumentOutOfRanfeException)
{ Console.WriteLine("ERROR")
}
return list[i].cost
Console.WriteLine("напиши количество {0} товара", i);
while (!int.TryParse(Console.ReadLine(), out list[i].amount))
{
Console.WriteLine("напиши количество {0} товара цифрой", i);
}
......}
static void Main()
{//тут вызовем метод shop}
Ответы (1 шт):
Автор решения: Igor
→ Ссылка
public static int NameToPrice(string name)
{
switch(name)
{
case "milk": return 10;
case "cola": return 7;
// Дальше перечисление
default: throw new ArgumentOutOfRangeException();
}
}
public static int Shop( ref Goods[] list)
{
...
for (int i = 0; i < MAX; i++)
{
...
//Goods [] listofGoods= new Goods[20];
list[i].cost = NameToPrice(list[i].name);
Надеюсь, что там нигде не опечаталась
Опечатались. Вы что, перепечатывали код в вопрос?
public static int NameToPrice(string name)
{
switch(name)
{
case "milk": return 10;
case "cola": return 7;
// Дальше перечисление
default: return -1;
}
}
public static int Shop( ref Goods[] list)
{
...
for (int i = 0; i < MAX; i++)
{
...
//Goods [] listofGoods= new Goods[20];
list[i].cost = NameToPrice(list[i].name);
if (list[i].cost == -1) {
Console.WriteLine("No such product. Try again.");
i--;
continue;
}