2016-12-02 3 views
0

私は文字列が空であるかどうかをチェックしたい最初のcaseが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を使用することはありませんが含まれているかどうかに応じて2つの異なるアクションを実行された場合。 if/else仕事のために構成されています

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

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

あなたには、いくつかの理由でswitchを使いたいならば、あなたはこのように、非空の状態のためdefaultを使用したいですどちらの方法もまったく同じことを行いますが、あなたが見ることができるように、if/elseはほんの少しシンプルです。

0

スイッチを使用する必要はありません。「If」が十分にあるはずです。

関連する問題