所以我試圖做一個程序,用戶被要求爲他們的選擇編寫一個數字。我已經聲明瞭我想要存儲他們的選擇的變量,但是我遇到了問題,因爲它表示變量沒有在程序中的任何地方使用。以下是幫助您理解的一些代碼。爲什麼這個變量不被識別? C#做 - 雖然問題
public class Rooms
{
public void roomNumber()
{
int userChoice;// The problem is here, c# is saying :
//Field Rooms.userChoice is never assigned to and will always have its default value 0
do
{
Console.WriteLine("What Type of Room ? ");
Console.WriteLine("Press 1 - Kitchen");
Console.WriteLine("Press 2 - Living");
Console.WriteLine("Press 3 - Bathroom");
Console.WriteLine("Press 4 - Bedroom");
Console.WriteLine("Press 5 If There Are No More Rooms To Add.");
userChoice = Convert.ToInt32(Console.ReadLine());
if(userChoice != 5)
{
Console.WriteLine("Is it Furnished/Finished ?");
Console.WriteLine("1 - Furnished");
Console.WriteLine("2 - Finished");
int status = Convert.ToInt32(Console.ReadLine());
}
} while (userChoice != 5);
可能是短暫的無效警告。關閉visual studio,刪除項目下的obj/bin文件夾,重新打開visual studio。如果它仍然存在,請忽略它,或者關閉BS導致它的任何代碼分析。 – Will
您確定這確實是產生該警告的代碼嗎?只有在重新聲明userChoice內部方法隱藏外部全局聲明 – Steve
顯示更多代碼時,才應該收到該消息。 – abelenky