2016-06-11 97 views
-3

我正在創建一個登錄終端,並且我有一個登錄字符串,所以我想檢查用戶是否正確輸出字符串的名稱,執行以下操作,這裏是代碼:檢查用戶輸入是否=定義的字符串

Console.WriteLine("--------------------"); 
Console.WriteLine("---LOGIN TERMINAL---"); 
Console.WriteLine("--------------------"); 
System.Threading.Thread.Sleep(1000); 
Console.WriteLine("/Log In"); 
Console.WriteLine("/Create New User"); 
Console.WriteLine("/Delete User"); 

userInput = Convert.ToString(Console.ReadLine()); 
if(userInput = LogIn) 
{ 

} 
+0

謝謝!我真的忘了,因爲我沒有代碼很長一段時間,謝謝反正! –

回答

0

= operator是一個賦值操作符。您需要使用等於運算符的== operator

if(userInput == LogIn) 
{ 

} 

如果LogIn不是一個變量,而是一個string,則需要用雙引號喜歡使用它;

if(userInput == "LogIn") 
{ 

} 
+0

通常VS給你一些提示這個陷阱.... –

+0

@FalcoAlexander是的,也有一些擴展來警告你,你沒有進行平等檢查,但分配。 –

0

這應有助於:

Console.WriteLine("--------------------"); 
Console.WriteLine("---LOGIN TERMINAL---"); 
Console.WriteLine("--------------------"); 
System.Threading.Thread.Sleep(1000); 
Console.WriteLine("/Log In"); 
Console.WriteLine("/Create New User"); 
Console.WriteLine("/Delete User"); 

var userInput = Convert.ToString(Console.ReadLine()); 
if(userInput == "Log In") // input check here 
{ 
    Console.WriteLine("Enter User Name"); 
    var userName = Console.ReadLine(); 
    Console.WriteLine("Enter User Password:"); 
    var password = Console.ReadLine(); 
    Console.WriteLine("User Name: {0}, Password: {1}", userName, password); 
} 
相關問題