What kind of conditions and statements can be used in the switch? Can you take user inputs? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What kind of conditions and statements can be used in the switch? Can you take user inputs?

example:switch (alert box appears and you input your age){case 1: console.writeline ("text here");break;case 2:console.writeline ("text here");break;}Is it possible to do something like this with the switch statement?

12th Jul 2016, 11:44 PM
Daniel Hildreth
Daniel Hildreth - avatar
4 Answers
0
Yes, but your example would only work if the age was 1 or 2. To have some response with each age, you need to put: case (specific numebr): DoSomethin(); break; i.e. you'd need to declare specific action ofr each spedicif age
13th Jul 2016, 12:15 PM
Tomek Cymes
Tomek Cymes - avatar
0
I mean is something like this possible? If so, I don't know what code I should use to take the user input. 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) { int age = alert("What is your age?"); switch (age) { case 16: Console.WriteLine("Too young"); break; case 42: Console.WriteLine("Adult"); break; case 70: Console.WriteLine("Senior"); break; default: Console.WriteLine("We were unable to determine your age. Please try again later."); break; } } } }
13th Jul 2016, 4:29 PM
Daniel Hildreth
Daniel Hildreth - avatar
0
That seems fine. Are you using Win forms or console? If console, then int age = Convert.ToInt32(Console.ReadLine()); Not sure how to do that in a window app though.
14th Jul 2016, 11:32 AM
Tomek Cymes
Tomek Cymes - avatar
0
Sorry Tomek. I worded my question wrong. I'm trying to find out how to code something like that. Do you know how I would code it? I tried using what I have up in the previous post, but it doesn't pop up with an alert box.
14th Jul 2016, 2:29 PM
Daniel Hildreth
Daniel Hildreth - avatar