0

Help needed. Used a switch statement in try catch block which worked but the error message wouldn't display. Thanks in advance.

A tour operator offers package holidays in England, Spain, Italy, Portugal and France. The program you are given defines an array with those options and takes N number as input. Write a program to output the package option with N index. If the number is out of range, program should output "Wrong number". Regardless of the option results, the program should output "Goodbye" at the end. Sample Input 2 Sample Output Italy Goodbye using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static System.Console; namespace SoloLearn {     class Program     {         static void Main(string[] args)         {             string[] tours = { "England", "Spain", "Italy", "Portugal", "France" };             int choice = Convert.ToInt32(Console.ReadLine());                          //your code goes here                      }     } }

15th Aug 2021, 8:34 AM
Cameron
5 Antworten
+ 3
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { string[] tours = { "England", "Spain", "Italy", "Portugal", "France" }; int choice = Convert.ToInt32(Console.ReadLine()); try { Console.WriteLine(tours[choice]); } catch { Console.WriteLine("Wrong number"); } finally { Console.WriteLine("Goodbye"); } } }}
21st May 2022, 1:37 PM
Gelbert Millones
+ 2
Cameron Morris Well done!
15th Aug 2021, 9:18 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Cameron Morris I had a look at your challenge and thought I might ask you to have a look at this solution. //your code goes here Console.Write(tours[choice]); Because you have an existing list of countries & an integer input, it makes sense to access the index of the list with the integer.
15th Aug 2021, 9:30 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Cameron Morris Please attach your attempt with your question so we can see what you are trying to do. PS: I don't think you need a try /except scenario. You could catch numbers outside the switch statements with a default statement
15th Aug 2021, 8:53 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
I solved my problem thanks to your advice! Really appreciate it. Thanks 👍
15th Aug 2021, 9:17 AM
Cameron