2016-12-02 66 views
0

我有一個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()); 

回答

0

如果你想要做的就是執行取決於sName是否包含一個空字符串或者不是兩個不同的動作,那麼我不會用switch/case可言。 if/else針對任務進行的:

if(sName == "") { 
    // sName is empty 
} else { 
    // sName is not empty 
} 

如果你想使用switch出於某種原因,你會使用default對於非空的情況下,像這樣:

switch(sName) { 
    case "": 
     // sName is empty 
     break; 
    default: 
     // sName is not empty 
     break; 
} 

無論哪種方式將做同樣的事情,但正如你可以看到if/else是比較簡單。

0

這聽起來像你不需要使用開關,「如果」應該足夠好

相關問題