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?
4 Respuestas
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
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;
}
}
}
}
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.
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.