Anyway I don't know how I'm supposed to go about doing this... in my application I need to have the switch repeat itself if there's an invalid input. All the application does is exit as soon as I enter a different result. Here's my code:
string str = Console.ReadLine();
char option = char.Parse(str);
//Need to repeat this switch:
switch (option)
{
case 'N':
Console.WriteLine("Creating New App...");
break;
case 'L':
Console.WriteLine("Loading App...");
break;
case 'O':
Console.WriteLine("Loading Options...");
break;
case 'Q':
Console.WriteLine("Exiting Application...");
System.Environment.Exit(1);
break;
default:
Console.WriteLine("Invalid input.");
break;
}
Console.ReadLine();
Put the switch in a loop
bool invalidInput = true;
while (invalidInput)
{
string str = Console.ReadLine();
char option = char.Parse(str);
switch (option)
{
case 'N':
Console.WriteLine("Creating New App...");
invalidInput = false;
break;
case 'L':
Console.WriteLine("Loading App...");
invalidInput = false;
break;
case 'O':
Console.WriteLine("Loading Options...");
invalidInput = false;
break;
case 'Q':
Console.WriteLine("Exiting Application...");
System.Environment.Exit(1);
break;
default:
Console.WriteLine("Invalid input.");
break;
}
}
You could make use of a while statement.
bool shouldRun = true;
while(shouldRun)
{
switch (option)
{
case 'N':
Console.WriteLine("Creating New App...");
shouldRun = false;
break;
case 'L':
Console.WriteLine("Loading App...");
shouldRun = false;
break;
case 'O':
Console.WriteLine("Loading Options...");
shouldRun = false;
break;
case 'Q':
Console.WriteLine("Exiting Application...");
System.Environment.Exit(1);
break;
default:
Console.WriteLine("Invalid input.");
shouldRun = true;
break;
}
}
To repeat the switch you need to use one loop.I think you are looking for something like the following:
bool ExitFlag = true;
while (ExitFlag)
{
ExitFlag = false;
switch (option)
{
case 'N':
Console.WriteLine("Creating New App...");
break;
case 'L':
Console.WriteLine("Loading App...");
break;
case 'O':
Console.WriteLine("Loading Options...");
break;
case 'Q':
Console.WriteLine("Exiting Application...");
System.Environment.Exit(1);
break;
default:
Console.WriteLine("Invalid input.");
ExitFlag = true;
break;
}
}
Note How It Works:
Let the ExitFlag be a Boolean value that controls the while loop( stop iteration and exit the while when ExitFlag is false). and are initialized with true. in each time the control enters into the while the flag is set to false so that we can avoid setting it false in multiple cases. The flag will set to true only when the default case is executed(ie., the invalid output) hence the loop repeats in this condition only.
You can use the Goto: label option in C#.
Ref: https://msdn.microsoft.com/en-us/library/13940fs2.aspx
ReadAgain:
string str = Console.ReadLine();
char option = char.Parse(str);
//The switch itself:
switch (option)
{
case 'N':
Console.WriteLine("Creating New App...");
break;
case 'L':
Console.WriteLine("Loading App...");
break;
case 'O':
Console.WriteLine("Loading Options...");
break;
case 'Q':
Console.WriteLine("Exiting Application...");
System.Environment.Exit(1);
break;
default:
Console.WriteLine("Invalid input.");
goto ReadAgain;
break;
}
Console.ReadLine();
Related
Is there any existing function/library to convert date and time format from moment.js to C#?
I will receive date and time format (only the format, without date and time value) from moment.js and need to display it in C#.
For example:
YYYY-MM-DD h:mm A
//this is the format received from moment.js
Then, if I have a DateTime in C#, I need to display it as
2018-06-14 2:30 PM
//this is equivalent with YYYY-MM-dd h:mm tt
I need something like this:
string ConvertToCustomDateTimeFormat(string momentJsFormat){
// should convert all moment.js date and time format to https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
}
then I could use it like this:
string dateTimeFormat = ConvertToCustomDateTimeFormat("YYYY-MM-DD h:mm A");
//dateTimeFormat value should be YYYY-MM-dd h:mm tt
string formattedDateTime = myDateTime.ToString(dateTimeFormat);
Console.WriteLine(formattedDateTime);
// output will be 2018-06-14 2:30 PM
I found a function to convert date and time format from C# to moment.js in here. So, I create a function to do the opposite (from moment.js to C#) based on that. I post it here in case someone also need it.
/// <summary>
/// Class to help convert moment.js date and time format to C# format
/// </summary>
public static class MomentJSConverter
{
private enum State
{
None,
LowerA,
CapitalA,
LowerD1,
LowerD2,
LowerD3,
LowerD4,
CapitalD1,
CapitalD2,
LowerH1,
LowerH2,
CapitalH1,
CapitalH2,
LowerM1,
LowerM2,
CapitalM1,
CapitalM2,
CapitalM3,
CapitalM4,
LowerS1,
LowerS2,
CapitalS1,
CapitalS2,
CapitalS3,
CapitalS4,
CapitalS5,
CapitalS6,
CapitalS7,
CapitalY1,
CapitalY2,
CapitalY3,
CapitalY4,
CapitalZ
}
public static string GenerateCSharpFormatString(string momentJsFormat)
{
StringBuilder resultBuilder = new StringBuilder();
State resultState = State.None;
StringBuilder tokenBuffer = new StringBuilder();
var ChangeState = new Action<State>((State fNewState) =>
{
switch (resultState)
{
case State.LowerA:
case State.CapitalA:
resultBuilder.Append("tt");
break;
case State.LowerD3:
resultBuilder.Append("ddd");
break;
case State.LowerD4:
resultBuilder.Append("dddd");
break;
case State.CapitalD1:
resultBuilder.Append("d");
break;
case State.CapitalD2:
resultBuilder.Append("dd");
break;
case State.LowerH1:
resultBuilder.Append("h");
break;
case State.LowerH2:
resultBuilder.Append("hh");
break;
case State.CapitalH1:
resultBuilder.Append("H");
break;
case State.CapitalH2:
resultBuilder.Append("HH");
break;
case State.LowerM1:
resultBuilder.Append("m");
break;
case State.LowerM2:
resultBuilder.Append("mm");
break;
case State.CapitalM1:
resultBuilder.Append("M");
break;
case State.CapitalM2:
resultBuilder.Append("MM");
break;
case State.CapitalM3:
resultBuilder.Append("MMM");
break;
case State.CapitalM4:
resultBuilder.Append("MMMM");
break;
case State.LowerS1:
resultBuilder.Append("s");
break;
case State.LowerS2:
resultBuilder.Append("ss");
break;
case State.CapitalS1:
resultBuilder.Append("f");
break;
case State.CapitalS2:
resultBuilder.Append("ff");
break;
case State.CapitalS3:
resultBuilder.Append("fff");
break;
case State.CapitalS4:
resultBuilder.Append("ffff");
break;
case State.CapitalS5:
resultBuilder.Append("fffff");
break;
case State.CapitalS6:
resultBuilder.Append("ffffff");
break;
case State.CapitalS7:
resultBuilder.Append("fffffff");
break;
case State.CapitalY2:
resultBuilder.Append("yy");
break;
case State.CapitalY4:
resultBuilder.Append("yyyy");
break;
case State.CapitalZ:
resultBuilder.Append("zzz");
break;
}
tokenBuffer.Clear();
resultState = fNewState;
});
foreach (var character in momentJsFormat)
{
switch (character)
{
case 'a':
switch (resultState)
{
case State.LowerA:
break;
default:
ChangeState(State.LowerA);
break;
}
break;
case 'A':
switch (resultState)
{
case State.CapitalA:
break;
default:
ChangeState(State.CapitalA);
break;
}
break;
case 'd':
switch (resultState)
{
case State.LowerD1:
resultState = State.LowerD2;
break;
case State.LowerD2:
resultState = State.LowerD3;
break;
case State.LowerD3:
resultState = State.LowerD4;
break;
case State.LowerD4:
break;
default:
ChangeState(State.LowerD1);
break;
}
break;
case 'D':
switch (resultState)
{
case State.CapitalD1:
resultState = State.CapitalD2;
break;
case State.CapitalD2:
break;
default:
ChangeState(State.CapitalD1);
break;
}
break;
case 'h':
switch (resultState)
{
case State.LowerH1:
resultState = State.LowerH2;
break;
case State.LowerH2:
break;
default:
ChangeState(State.LowerH1);
break;
}
break;
case 'H':
switch (resultState)
{
case State.CapitalH1:
resultState = State.CapitalH2;
break;
case State.CapitalH2:
break;
default:
ChangeState(State.CapitalH1);
break;
}
break;
case 'm':
switch (resultState)
{
case State.LowerM1:
resultState = State.LowerM2;
break;
case State.LowerM2:
break;
default:
ChangeState(State.LowerM1);
break;
}
break;
case 'M':
switch (resultState)
{
case State.CapitalM1:
resultState = State.CapitalM2;
break;
case State.CapitalM2:
resultState = State.CapitalM3;
break;
case State.CapitalM3:
resultState = State.CapitalM4;
break;
case State.CapitalM4:
break;
default:
ChangeState(State.CapitalM1);
break;
}
break;
case 's':
switch (resultState)
{
case State.LowerS1:
resultState = State.LowerS2;
break;
case State.LowerS2:
break;
default:
ChangeState(State.LowerS1);
break;
}
break;
case 'S':
switch (resultState)
{
case State.CapitalS1:
resultState = State.CapitalS2;
break;
case State.CapitalS2:
resultState = State.CapitalS3;
break;
case State.CapitalS3:
resultState = State.CapitalS4;
break;
case State.CapitalS4:
resultState = State.CapitalS5;
break;
case State.CapitalS5:
resultState = State.CapitalS6;
break;
case State.CapitalS6:
resultState = State.CapitalS7;
break;
case State.CapitalS7:
break;
default:
ChangeState(State.CapitalS1);
break;
}
break;
case 'Y':
switch (resultState)
{
case State.CapitalY1:
resultState = State.CapitalY2;
break;
case State.CapitalY2:
resultState = State.CapitalY3;
break;
case State.CapitalY3:
resultState = State.CapitalY4;
break;
case State.CapitalY4:
break;
default:
ChangeState(State.CapitalY1);
break;
}
break;
case 'Z':
switch (resultState)
{
case State.CapitalZ:
break;
default:
ChangeState(State.CapitalZ);
break;
}
break;
default:
ChangeState(State.None);
resultBuilder.Append(character);
break;
}
}
ChangeState(State.None);
return resultBuilder.ToString();
}
}
I added support for literal strings.
public static class MomentJSConverter
{
private enum State
{
None,
LowerA,
CapitalA,
LowerD1,
LowerD2,
LowerD3,
LowerD4,
CapitalD1,
CapitalD2,
LowerH1,
LowerH2,
CapitalH1,
CapitalH2,
LowerM1,
LowerM2,
CapitalM1,
CapitalM2,
CapitalM3,
CapitalM4,
LowerS1,
LowerS2,
CapitalS1,
CapitalS2,
CapitalS3,
CapitalS4,
CapitalS5,
CapitalS6,
CapitalS7,
CapitalY1,
CapitalY2,
CapitalY3,
CapitalY4,
CapitalZ
}
private static string InnerGenerateCSharpFormatString(string momentJsFormat)
{
StringBuilder resultBuilder = new StringBuilder();
State resultState = State.None;
StringBuilder tokenBuffer = new StringBuilder();
var ChangeState = new Action<State>((State fNewState) =>
{
switch (resultState)
{
case State.LowerA:
case State.CapitalA:
resultBuilder.Append("tt");
break;
case State.LowerD3:
resultBuilder.Append("ddd");
break;
case State.LowerD4:
resultBuilder.Append("dddd");
break;
case State.CapitalD1:
resultBuilder.Append("d");
break;
case State.CapitalD2:
resultBuilder.Append("dd");
break;
case State.LowerH1:
resultBuilder.Append("h");
break;
case State.LowerH2:
resultBuilder.Append("hh");
break;
case State.CapitalH1:
resultBuilder.Append("H");
break;
case State.CapitalH2:
resultBuilder.Append("HH");
break;
case State.LowerM1:
resultBuilder.Append("m");
break;
case State.LowerM2:
resultBuilder.Append("mm");
break;
case State.CapitalM1:
resultBuilder.Append("M");
break;
case State.CapitalM2:
resultBuilder.Append("MM");
break;
case State.CapitalM3:
resultBuilder.Append("MMM");
break;
case State.CapitalM4:
resultBuilder.Append("MMMM");
break;
case State.LowerS1:
resultBuilder.Append("s");
break;
case State.LowerS2:
resultBuilder.Append("ss");
break;
case State.CapitalS1:
resultBuilder.Append("f");
break;
case State.CapitalS2:
resultBuilder.Append("ff");
break;
case State.CapitalS3:
resultBuilder.Append("fff");
break;
case State.CapitalS4:
resultBuilder.Append("ffff");
break;
case State.CapitalS5:
resultBuilder.Append("fffff");
break;
case State.CapitalS6:
resultBuilder.Append("ffffff");
break;
case State.CapitalS7:
resultBuilder.Append("fffffff");
break;
case State.CapitalY2:
resultBuilder.Append("yy");
break;
case State.CapitalY4:
resultBuilder.Append("yyyy");
break;
case State.CapitalZ:
resultBuilder.Append("zzz");
break;
}
tokenBuffer.Clear();
resultState = fNewState;
});
foreach (var character in momentJsFormat)
{
switch (character)
{
case 'a':
switch (resultState)
{
case State.LowerA:
break;
default:
ChangeState(State.LowerA);
break;
}
break;
case 'A':
switch (resultState)
{
case State.CapitalA:
break;
default:
ChangeState(State.CapitalA);
break;
}
break;
case 'd':
switch (resultState)
{
case State.LowerD1:
resultState = State.LowerD2;
break;
case State.LowerD2:
resultState = State.LowerD3;
break;
case State.LowerD3:
resultState = State.LowerD4;
break;
case State.LowerD4:
break;
default:
ChangeState(State.LowerD1);
break;
}
break;
case 'D':
switch (resultState)
{
case State.CapitalD1:
resultState = State.CapitalD2;
break;
case State.CapitalD2:
break;
default:
ChangeState(State.CapitalD1);
break;
}
break;
case 'h':
switch (resultState)
{
case State.LowerH1:
resultState = State.LowerH2;
break;
case State.LowerH2:
break;
default:
ChangeState(State.LowerH1);
break;
}
break;
case 'H':
switch (resultState)
{
case State.CapitalH1:
resultState = State.CapitalH2;
break;
case State.CapitalH2:
break;
default:
ChangeState(State.CapitalH1);
break;
}
break;
case 'm':
switch (resultState)
{
case State.LowerM1:
resultState = State.LowerM2;
break;
case State.LowerM2:
break;
default:
ChangeState(State.LowerM1);
break;
}
break;
case 'M':
switch (resultState)
{
case State.CapitalM1:
resultState = State.CapitalM2;
break;
case State.CapitalM2:
resultState = State.CapitalM3;
break;
case State.CapitalM3:
resultState = State.CapitalM4;
break;
case State.CapitalM4:
break;
default:
ChangeState(State.CapitalM1);
break;
}
break;
case 's':
switch (resultState)
{
case State.LowerS1:
resultState = State.LowerS2;
break;
case State.LowerS2:
break;
default:
ChangeState(State.LowerS1);
break;
}
break;
case 'S':
switch (resultState)
{
case State.CapitalS1:
resultState = State.CapitalS2;
break;
case State.CapitalS2:
resultState = State.CapitalS3;
break;
case State.CapitalS3:
resultState = State.CapitalS4;
break;
case State.CapitalS4:
resultState = State.CapitalS5;
break;
case State.CapitalS5:
resultState = State.CapitalS6;
break;
case State.CapitalS6:
resultState = State.CapitalS7;
break;
case State.CapitalS7:
break;
default:
ChangeState(State.CapitalS1);
break;
}
break;
case 'Y':
switch (resultState)
{
case State.CapitalY1:
resultState = State.CapitalY2;
break;
case State.CapitalY2:
resultState = State.CapitalY3;
break;
case State.CapitalY3:
resultState = State.CapitalY4;
break;
case State.CapitalY4:
break;
default:
ChangeState(State.CapitalY1);
break;
}
break;
case 'Z':
switch (resultState)
{
case State.CapitalZ:
break;
default:
ChangeState(State.CapitalZ);
break;
}
break;
default:
ChangeState(State.None);
resultBuilder.Append(character);
break;
}
}
ChangeState(State.None);
return resultBuilder.ToString();
}
public static string GenerateCSharpFormatString(string momentJsFormat){
string[] subStrs = Regex.Split(momentJsFormat, #"(\[[^\]]+\])");
var res = new StringBuilder();
foreach (var subStr in subStrs){
if(subStr.Contains("[")){
res.Append(Regex.Replace(subStr, #"[\[\]]", "'"));
}else{
res.Append(InnerGenerateCSharpFormatString(subStr));
}
}
return res.ToString();
}
}
I'm working a little with switch statements and want to know how to ignore the case sensitivity when it comes to input values.
Here is my code:
using System;
namespace SwitchStatements
{
class MainClass
{
public static void Main(string[] args)
{
Start:
Console.WriteLine("Please Input the Grade");
char grade = Convert.ToChar(Console.ReadLine());
switch (grade)
{
case 'A':
Console.WriteLine("Excellent Work!");
break;
case 'B':
Console.WriteLine("Very Good Effort! Just a couple of Errors =)");
break;
case 'C':
Console.WriteLine("You Passed. Push Yourself Next Time");
break;
case 'D':
Console.WriteLine("Better put in more effort next time. I know you can do better");
break;
default:
Console.WriteLine("Invalid Grade.");
break;
}
Console.ReadKey();
goto Start;
}
}
}
If I put 'a' in instead of 'A' it returns the default response.
Can I use perhaps a .Comparison of some sort? If so where would I put it?
You can use ConsoleKey as condition for switch, the code will be like the following.
var grade =Console.ReadKey().Key;
switch (grade)
{
case ConsoleKey.A:
Console.WriteLine("Excellent Work!");
break;
case ConsoleKey.B:
// Something here
break;
case ConsoleKey.C:
// Something here
break;
case ConsoleKey.D:
// Something here
break;
case ConsoleKey.E:
// Something here
break;
default:
// Something here
break;
}
So that you can avoid converting the input to uppercase/Lower case, and then it goes for another conversion To Char. Simply use ConsoleKey Enumeration inside the switch.
You can use ToUpper(); Like
Convert.ToChar(Console.ReadLine().ToUpper());
and to get saved from the error of getting more charaters with Console.ReadLine() you can use
char grd = Convert.ToChar(Console.ReadKey().KeyChar.ToString().ToUpper());
you can use like following also
char grade = Convert.ToChar(Console.ReadLine().ToUpperInvariant());
https://msdn.microsoft.com/en-us/library/system.string.toupperinvariant.aspx
Convert to uppercase before switch like below,
grade = Char.ToUpper(grade);
Change
char grade = Convert.ToChar(Console.ReadLine());
To
char grade = Convert.ToChar(Console.ReadLine().ToUpper());
https://msdn.microsoft.com/en-us/library/system.string.toupper(v=vs.110).aspx
Write Switch on grade.ToUpper() like this and don't change change it's value, may be you will need it after
char grade = Convert.ToChar(Console.ReadLine());
switch (grade.ToUpper())
{
case 'A':
Console.WriteLine("Excellent Work!");
break;
case 'B':
Console.WriteLine("Very Good Effort! Just a couple of Errors =)");
break;
case 'C':
Console.WriteLine("You Passed. Push Yourself Next Time");
break;
case 'D':
Console.WriteLine("Better put in more effort next time. I know you can do better");
break;
default:
Console.WriteLine("Invalid Grade.");
break;
}
You may fall from one case to another like this
public static void Main(string[] args)
{
Boolean validInputRead = false;
Char grade;
while(!validInputRead)
{
validInputRead = true;
Console.WriteLine("Please Input the Grade");
grade = Convert.ToChar(Console.Read());
switch (grade)
{
case 'A':
case 'a':
Console.WriteLine("Excellent Work!");
break;
case 'B':
case 'b':
Console.WriteLine("Very Good Effort! Just a couple of Errors =)");
break;
case 'C':
case 'c':
Console.WriteLine("You Passed. Push Yourself Next Time");
break;
case 'D':
case 'd':
Console.WriteLine("Better put in more effort next time. I know you can do better");
break;
default:
Console.WriteLine("Invalid Grade.");
validInputRead = false;
break;
}
Console.ReadKey();
}
}
EDIT
Changed from Console.ReadLine() to Console.Read() as suggested
Added while(!validInputRead) as requested
string letterGrade;
int grade = 0;
// This will hold the final letter grade
Console.Write("Input the grade :");
switch (grade)
{
case 1:
// 90-100 is an A
letterGrade = "A";
Console.WriteLine("grade b/n 90-100");
break;
case 2:
// 80-89 is a B
letterGrade = "B";
Console.WriteLine("grade b/n 80-89");
break;
case 3:
// 70-79 is a C
letterGrade = "C";
Console.WriteLine("grade b/n 70-79");
break;
case 4:
// 60-69 is a D
letterGrade = "D";
Console.WriteLine(" grade b/n 60-69 ");
break;
default:
// point whic is less than 59
Console.WriteLine("Invalid grade");
break;
}
When I'm doing the code without the goto command it works, but when I add the :Start it get an 8 error.
Here is the relevant code:
:Start
Console.Write("Do you want the yes or no?");
string what = Console.ReadLine();
switch (what)
{
case "yes":
Console.WriteLine("You choose yes");
break;
case "no":
Console.WriteLine("You choose no");
break;
default:
Console.WriteLine("{0},is not a word",what);
goto Start;
}
The correct syntax is Start:. But, instead of goto, you should set this up in a loop:
bool invalid = true;
while (invalid)
{
Console.Write("Do you want the yes or no?");
string what = Console.ReadLine();
switch (what)
{
case "yes":
Console.WriteLine("You choose yes");
invalid = false;
break;
case "no":
Console.WriteLine("You choose no");
invalid = false;
break;
default:
Console.WriteLine("{0},is not a word",what);
}
}
Try "Start:" instead of ":Start"
like this:
Start:
Console.Write("Do you want the yes or no?");
string what = Console.ReadLine();
switch (what)
{
case "yes":
Console.WriteLine("You choose yes");
break;
case "no":
Console.WriteLine("You choose no");
break;
default:
Console.WriteLine("{0},is not a word", what);
goto Start;
}
http://msdn.microsoft.com/en-us/library/aa664740(v=vs.71).aspx
The correct syntax for a label is Start:, not :Start
You can refactor your code to omit the goto statement instead (better):
bool continue = true;
while (continue) {
Console.Write("Do you want the yes or no?");
string what = Console.ReadLine();
switch (what)
{
case "yes":
Console.WriteLine("You choose yes");
continue = false;
break;
case "no":
Console.WriteLine("You choose no");
continue = false;
break;
default:
Console.WriteLine("{0}, is not a word",what);
break;
}
}
I'm trying to determine the best way to get the following code to iterate until the user enters a valid number(1-6). As long as the user inputs a number that is greater than 6, the program should continue to prompt the user for a valid number.
static void Main(string[] args)
{
string favoriteBand;
Console.WriteLine("What is your favorite rock band?");
Console.WriteLine("");
Console.WriteLine("1.) Journey");
Console.WriteLine("2.) Boston");
Console.WriteLine("3.) Styx");
Console.WriteLine("4.) Kansas");
Console.WriteLine("5.) Foreigner");
Console.WriteLine("Press 6 to exit the program.");
Console.WriteLine("");
favoriteBand = (Console.ReadLine());
switch (favoriteBand)
{
case "1": Console.WriteLine("Don't Stop Believin'!"); break;
case "2": Console.WriteLine("More Than a Feeling!"); break;
case "3": Console.WriteLine("Come Sail Away!"); break;
case "4": Console.WriteLine("Dust in the Wind!"); break;
case "5": Console.WriteLine("Hot Blooded!"); break;
case "6": return;
default: Console.WriteLine("Error, invalid choice. Please choose a valid number."); break;
}
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
}
How can I get this program to continue prompting for a valid number using a loop?
Just for fun, here we can remove the switch
(not really needed for a simple switch with only 6 cases)
bool isValid = true;
Dictionary<string, Action> command = new Dictionary<string, Action>();
command.Add("1", () => {Console.WriteLine("Don't Stop Believin'!");isValid = false;});
command.Add("2", () => {Console.WriteLine("More Than a Feeling!");isValid = false;});
command.Add("3", () => {Console.WriteLine("Come Sail Away!");isValid = false;});
command.Add("4", () => {Console.WriteLine("Dust in the Wind!");isValid = false;});
command.Add("5", () => {Console.WriteLine("Hot Blooded!");isValid = false;});
command.Add("6", () => isValid = false);
while(isValid)
{
string line = Console.ReadLine();
if(command.Keys.Contains(line))
command[line].Invoke();
else
Console.WriteLine("Choose from 1 to 6");
}
Use a bool variable with while
bool control = true;
while(control)
{
favoriteBand = (Console.ReadLine());
switch (favoriteBand)
{
case "1": Console.WriteLine("Don't Stop Believin'!"); control = false;break;
case "2": Console.WriteLine("More Than a Feeling!");control = false; break;
case "3": Console.WriteLine("Come Sail Away!"); control = false;break;
case "4": Console.WriteLine("Dust in the Wind!"); control = false;break;
case "5": Console.WriteLine("Hot Blooded!"); control = false;break;
case "6": control = false; break;
default: Console.WriteLine("Error, invalid choice. Please choose a valid number."); break;
}
}
Given the following code snippet:
case "add":
goto add();
return;
case "subtract":
goto subtract();
return;
case "multiply":
goto multiply();
return;
case "division":
goto divide();
return;
default:
{
Console.WriteLine("Invalid choice"):
}
break;
Using switch case would I be able to use the goto keyword to take me to the function I want? If not, how would I go about achieving this?
You can't use the goto keyword, just call the method.
So
string methodName = Foo.GetMethodNameToCall();
switch(methodName)
{
case "add":
add();
break;
case "subtract":
subtract();
break;
case "multiply":
multiply();
break;
case "division":
divide();
break;
default:
{
Console.WriteLine("Invalid choice"):
break;
}
}
You probably want to use break instead of return in your cases also.