我有一個switch語句,第一種情況是「」,因爲我想檢查字符串是否爲空。我的問題是我需要另一個能夠檢查變量'sName'中是否有內容的案例,但我不知道如何寫出來。我有一個switch語句,但我不知道如何將其更改爲我的需要
您可以忽略案件中的內容,但包含破折號的粗體案例是我需要更改的案例。
(相關代碼)
string sName = "";
int iChoice = 0;
switch (sName)
{
case "":
Console.Clear();
Console.WriteLine("You are not yet logged in, would you like to create a new account?\n");
Console.WriteLine("------------------------------------------------------------------------------------------------------------------");
Console.WriteLine("PLEASE NOTE: IF YOU ATTEMPT TO CREATE A NEW ACCOUNT WITH AN EXISTING NAME, YOUR PREVIOUS DATA WILL BE OVERWRITTEN!");
Console.WriteLine("------------------------------------------------------------------------------------------------------------------");
Console.WriteLine("\n1. Create a new account");
Console.WriteLine("2. Login");
iChoice = Convert.ToInt32(Console.ReadLine());
switch (iChoice)
{
case 1:
//outputs text to the console
Console.Write("What is your name? ");
//stores user input into variable sName
sName = Console.ReadLine();
//outputs text to the console
Console.Write("\nWhich school do you work at? ");
//stores user input into variable sSchool
sSchool = Console.ReadLine();
//outputs text to the console
Console.Write("\nWhat class do you teach? ");
//stores user input into variable sClass
sClass = Console.ReadLine();
//outputs the user's name
Console.WriteLine("\nYou are now logged in as " + sName + "\n");
//creates a new text file using the users name
using (StreamWriter sw = new StreamWriter(sName + ".txt"))
{
//writes to the text file using the variables
sw.WriteLine(sName);
sw.WriteLine(sSchool);
sw.WriteLine(sClass);
}
Console.Clear();
displayMenu();
iOption = Convert.ToInt32(Console.ReadLine());
break;
case 2:
//outputs text to the console
Console.Write("What is your name? ");
//stores user input into variable sName
sName = Console.ReadLine();
Console.Clear();
displayMenu();
iOption = Convert.ToInt32(Console.ReadLine());
break;
}
Console.ReadKey();
Console.Clear();
displayMenu();
iOption = Convert.ToInt32(Console.ReadLine());
break;
***case --------------------------------:***
{
Console.Clear();
Console.WriteLine("You are already logged in as " + sName);
Console.WriteLine("You are not yet logged in, would you like to create a new account?\n");
Console.WriteLine("------------------------------------------------------------------------------------------------------------------");
Console.WriteLine("PLEASE NOTE: IF YOU ATTEMPT TO CREATE A NEW ACCOUNT WITH AN EXISTING NAME, YOUR PREVIOUS DATA WILL BE OVERWRITTEN!");
Console.WriteLine("------------------------------------------------------------------------------------------------------------------");
Console.WriteLine("\n1. Create a new account");
Console.WriteLine("2. Login");
iChoice = Convert.ToInt32(Console.ReadLine());