그래서 사용자가 주어진 선택 항목에 숫자를 쓰도록 요청하는 프로그램을 만들려고합니다. 변수를 어디에 저장하고 싶은지 선언했는데 변수가 어디서나 사용되는 것은 아니기 때문에 문제가 있습니다. 다음은 이해를 돕기위한 몇 가지 코드입니다.이 변수가 인식되지 않는 이유는 무엇입니까? C# Do-While 문제
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
이것이 해당 경고를 생성하는 코드와 정확히 일치합니까? 외부 글로벌 선언 – Steve
더 많은 코드 표시를 모호하게하는 메서드 내에서 userChoice를 다시 선언하는 경우에만 해당 메시지를 가져와야합니다. – abelenky