Switch Statements | C# | Tutorial 17
Source Code - www.giraffeacademy.com/program... This video is one in a series of videos where we'll be looking at programming in C#. The course is designed for new programmers, and will introduce common programming topics using the C# language.
Throughout the course we'll be looking at various topics including variables, arrays, getting user input, loops, conditionals, object orientation and much more.
Пікірлер: 23
Me during the video: seems like the same thing you could do with if and if else statements. Him at the end: They do the same thing but this much neater and easier to read, etc. Me: :O He is right! Time to evolve. Thanks again.
@imagine9616
3 жыл бұрын
me too
Still learning with your c# tutorials!!! Thank you so much!!
I decided to add user input (using a string) and returning to display it: static void Main(string[] args) { //LESSON 15 Console.WriteLine("LESSON 15"); Console.WriteLine(""); Console.WriteLine(GetDay() + " is what you selected."); //Display weekday name selected to user. Console.ReadLine(); } static string GetDay() { Console.Write("Enter a number between 0-7: "); string userNum = Console.ReadLine(); //Get user number string dayName; //Weekday names switch (userNum) //Compare user input to accepted string value { case "0": dayName = "Sunday"; return (dayName); //Return results to main function/method. case "1": dayName = "Monday"; return dayName; case "2": dayName = "Tuesday"; return dayName; case "4": dayName = "Wednesday"; return dayName; case "5": dayName = "Thursday"; return dayName; case "6": dayName = "Friday"; return dayName; case "7": dayName = "Saturday"; return dayName; default: dayName = "Invalid Number. Please try again."; return dayName; } }
do you have the video that used if statement please?
Not sure if you read comments but thanks also love the hair
building a calculator using switch statements is better than using if statements i guess, what do you think ?
@imagine9616
3 жыл бұрын
probably
it says "Use of unassigned local variable 'dayname'" can someone help me? im new to this. I did the exact same thing why did it not work for me? pls help.
@upcom1ng116
2 жыл бұрын
If that error occurred on return then likely you forgot default in switch statement.
Monday is the first day of the week. Sunday is in the weekEND.
@upcom1ng116
2 жыл бұрын
Depending where you are from and which calendar you are using though.
@siegeofhyrule8640
2 жыл бұрын
False, Saturday is the weekend. Sunday is the day of rest and the first day of the week.
@maxcohen13
2 жыл бұрын
@@siegeofhyrule8640 So you start the week by not doing anything. That makes a lot of sense. Being stupid is not a contest. There are no prizes.
@siegeofhyrule8640
2 жыл бұрын
@@maxcohen13 Sunday being a day of Rest is traditional. I was explaining why it might be considered a weekend by the School system. I never said that it was Rational. and frankly if you are smart you will work through Sunday. and also check your calendar, Sunday is at the beginning.
@siegeofhyrule8640
2 жыл бұрын
And also calling names isn't smart, it only causes arguments and grief, maybe try avoiding that next time you are commenting on the internet.
you haven't shown the process of user input. what if it's required to the user to input a number? thanks
@simonvandenhende5274
4 жыл бұрын
you can get the user's input like this: Console.Write("Enter any number >> "); string input = Console.ReadLine(); int number = Convert.ToInt32(input); GetDay(number); Note that this assumes the user typed in a valid integer number. If you want to be extra careful you can do the following. Console.Write("Enter any number >> "); string input = Console.ReadLine(); int number = 0; if (Int32.TryParse(input, out number)) { GetDay(number); } else { Console.WriteLine("Invalid input, emphasis on number."); ask_number(); }
@leobryant1011
3 жыл бұрын
he showed how to get user input in a previous video
@imagine9616
3 жыл бұрын
watch his previous videos
@AnimeRPG1
3 жыл бұрын
Could do something like this (its what I did after the video ended and decided to try and add it): static void Main(string[] args) { //LESSON 15 Console.WriteLine("LESSON 15"); Console.WriteLine(""); Console.WriteLine(GetDay() + " is what you selected."); //Display weekday name selected by user. Console.ReadLine(); } static string GetDay() { Console.Write("Enter a number between 0-7: "); string userNum = Console.ReadLine(); //Get user number string dayName; //Weekday names switch (userNum) //Compare user input to accepted string value { case "0": dayName = "Sunday"; return (dayName); //Return results to main function/method. case "1": dayName = "Monday"; return dayName; case "2": dayName = "Tuesday"; return dayName; case "4": dayName = "Wednesday"; return dayName; case "5": dayName = "Thursday"; return dayName; case "6": dayName = "Friday"; return dayName; case "7": dayName = "Saturday"; return dayName; default: dayName = "Invalid Number. Please try again."; return dayName; } }
SuNdAy iS ThE FiRsT dAy oF tHe WeEk,