2017-02-28 34 views
0

我正在製作一個小遊戲來幫助我學習c#。到目前爲止,我所做的全部都添加了一些變量,如fullName,firstName和lastName,以便稍後可以引用它們。這工作得很好。但是當我想到添加諸如Mr.或Ms.這樣的名稱前綴(我在代碼中將它們稱爲「標題」)的問題時,控制檯只是打開了空白。這裏是我的代碼...當運行我的基本c#控制檯應用程序時,我的控制檯打開空白,我不知道爲什麼

using System; 

namespace My_Fist_Console_Game 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string userValue = Console.ReadLine(); 

      Console.WriteLine("Airport Sim: Console Edition (literally)"); 
      Console.Write("Type your first name here: "); 
      string firstName = Console.ReadLine(); 
      Console.Write("Type your last name here: "); 
      string lastName = Console.ReadLine(); 

      string title; 

      Console.Write("are you male or female?"); 
      if ((userValue == "male") || (userValue == "Male")) 
      { 
       title = "Mr. "; 
      } 
      else if ((userValue == "female") || (userValue == "Female")) 
      { 
       title = "Ms. "; 
      } 

      string fullName = firstName + " " + lastName; 

      Console.ReadLine(); 

      Console.Clear(); 

      Console.WriteLine("Flight Attendant: "); 
      Console.WriteLine(" Hello " + title + " " + lastName); 





      Console.ReadLine(); 
     } 
    } 
} 

回答

1

第一條語句在你的程序是Console.ReadLine它等待用戶輸入的東西。由於您從未告訴它在之前打印到控制檯,因此控制檯打開並顯示爲空白。

如果您輸入內容並點擊「Enter」,則代碼可能會繼續執行,您會看到輸出。

+0

謝謝!現在我刪除了「userValue = Console.ReadLine();」,我該怎麼做,以便if語句中的userValue仍然可以識別男性,男性,女性等? – Valorum

+0

請參閱Willy的答案,瞭解如何對其進行修改以便仍然有效 – BradleyDotNET

0

把你的情況你上面的uservalue變量:

string userValue = Console.ReadLine(); 
     if ((userValue == "male") || (userValue == "Male")) 
     { 
      title = "Mr. "; 
     } 

然後嘗試編輯這個部分:

string fullName = firstName + " " + lastName; 

     Console.ReadLine(); //Remove this and put it after Console.WriteLine 

     Console.Clear(); //Remove this and put it after Console.WriteLine 

     Console.WriteLine("Flight Attendant: "); 
     Console.WriteLine(" Hello " + " " + lastName); 

你在做什麼是你把到Console.ReadLine(),這將指望任何在程序發生任何事情之前先從用戶輸入。

1
using System; 

namespace My_Fist_Console_Game 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine("Airport Sim: Console Edition (literally)"); 
      Console.Write("Type your first name here: "); 
      string firstName = Console.ReadLine(); 
      Console.Write("Type your last name here: "); 
      string lastName = Console.ReadLine(); 

      string title; 
      while (title != "Mr. " && title != "Ms. ") 


      { 
       Console.Write("are you male or female? Input male, if you are male  or imput female if you are female"); 

       string userValue = Console.ReadLine(); 
       if ((userValue == "male") || (userValue == "Male")) 
       { 
        title = "Mr. "; 
       } 
       else if ((userValue == "female") || (userValue == "Female")) 
       { 
        title = "Ms. "; 
       } 
      } 
      string fullName = firstName + " " + lastName; 

      Console.ReadLine(); 

      Console.Clear(); 

      Console.WriteLine("Flight Attendant: "); 
      Console.WriteLine(" Hello " + title + " " + lastName); 
      Console.ReadLine(); 
     } 
    } 
} 
相關問題