Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Решение със switch:
- using System;
- namespace FruitOrVegetable
- {
- class Program
- {
- static void Main(string[] args)
- {
- string product = Console.ReadLine();
- switch (product)
- {
- case "banana":
- case "apple":
- case "kiwi":
- case "cherry":
- case "lemon":
- case "grapes":
- Console.WriteLine("fruit");
- break;
- case "tomato":
- case "cucumber":
- case "pepper":
- case "carrot":
- Console.WriteLine("vegetable");
- break;
- default:
- Console.WriteLine("unknown");
- break;
- }
- }
- }
- }
- Решение с if-else:
- using System;
- namespace FruitOrVegetable
- {
- class Program
- {
- static void Main(string[] args)
- {
- string product = Console.ReadLine();
- if (product == "banana" || product == "apple" || product == "kiwi" || product == "cherry" || product == "lemon" || product == "grapes")
- {
- Console.WriteLine("fruit");
- }
- else if (product == "tomato" || product == "cucumber" || product == "pepper" || product == "carrot")
- {
- Console.WriteLine("vegetable");
- }
- else
- {
- Console.WriteLine("unknown");
- }
- }
- }
- }
- Решение с тернарен оператор:
- using System;
- namespace FruitOrVegetable
- {
- class Program
- {
- static void Main(string[] args)
- {
- string product = Console.ReadLine();
- Console.WriteLine( product == "banana" || product == "apple" || product == "kiwi"|| product == "cherry" || product == "lemon" || product == "grapes" ? "fruit" : product == "tomato" || product == "cucumber" || product == "pepper" || product == "carrot" ? "vegetable" : "unknown");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement