Nested Conditional Statements - Lab - 06. Fruit Shop
Здавейте колеги,
Имах проблеми с решението на тази задача:
function fruitShop(input) { let fruit = (input.shift()).toLowerCase(); let day = (input.shift()).toLowerCase(); let quantity = Number(input.shift()); let price = 0; if (day == "Monday" || day == "Tuesday" || day == "Wednesday" || day == "Thursday" || day == "Friday") { if (fruit == "banana") { price = quantity * 2.50; } else if (fruit == "apple") { price = quantity * 1.20; } else if (fruit == "orange") { price = quantity * 0.85; } else if (fruit == "grapefruit") { price = quantity * 1.45; } else if (fruit == "kiwi") { price = quantity * 2.70; } else if (fruit == "pineapple") { price = quantity * 5.50; } else { price = quantity * 3.85; } } else if (day == "Saturday" || day == "Sunday") { if (fruit == "banana") { price = quantity * 2.70 } else if (fruit == "apple") { price = quantity * 1.25; } else if (fruit == "orange") { price = quantity * 0.90; } else if (fruit == "grapefruit") { price = quantity * 1.60; } else if (fruit == "kiwi") { price = quantity * 3.00; } else if (fruit == "pineapple") { price = quantity * 5.60; } else { price = quantity * 4.20; } } if (price > 0) { console.log("${(price * quantity).toFixed(2)}"); } else { console.log("error"); } }
Някой би ли могъл да ми даде правилното решение, за да сравня?
Бихте ли ми помогнали за моята програма ...защо не изписва "error"
string fruit = Console.ReadLine();
string weekday = Console.ReadLine();
double num = double.Parse(Console.ReadLine());
double price = 0;
bool doing = false;
if((weekday == "Monday") || (weekday == "Tuesday") || (weekday == "Wednesday") || (weekday == "Thuesday") || (weekday == "Friday"))
{
if(fruit == "banana")
{ price = 2.50; doing = true;}
else if(fruit == "apple")
{ price = 1.20; doing = true;}
else if(fruit == "orange")
{ price = 0.85; doing = true;}
else if(fruit == "grapefruit")
{ price = 1.45; doing = true;}
else if(fruit == "kiwi")
{ price = 2.70; doing = true;}
else if(fruit == "pineapple")
{ price = 5.50; doing = true;}
else if(fruit == "grapes")
{ price = 3.85; doing = true;}
}
else if((weekday == "Saturday") || (weekday == "Sunday"))
{
if(fruit == "banana")
{ price = 2.70; doing = true;}
else if(fruit == "apple")
{ price = 1.25; doing = true;}
else if(fruit == "orange")
{ price = 0.90; doing = true;}
else if(fruit == "grapefruit")
{ price = 1.60; doing = true;}
else if(fruit == "kiwi")
{ price = 3.00; doing = true;}
else if(fruit == "pineapple")
{ price = 5.60; doing = true;}
else if(fruit == "grapes")
{ price = 4.20; doing = true; }
}
else
{
Console.WriteLine("error");
}
if(doing == true)
{
price = price * num;
Console.WriteLine("{0:F2}", price);
}
Здравей,
Изписва error само ако деня не е правилен. Трябва да имаш и проверка за плодовете. След else if(fruit == "grapes"){ price = 3.85; doing = true;} и else if(fruit == "grapes"){ price = 4.20; doing = true; } => else {Console.WriteLine("error");}
Благодаря Ви !
Получи се !