2017-12-19 17 views
0

작은 시네마 프로그램을 만들려고합니다. 사용자 나이에 따라 if 문을 설정하고 하나의 나이는 12A입니다. 즉, 그들이 동행하는지 묻지 않으면 안됩니다. 성인프로그램에서 부울 조건을 올바르게 설정하는 방법

Console.WriteLine(" Enter the number of the film you wish to see :"); 

int selection = int.Parse(Console.ReadLine()); 
Console.WriteLine("Enter your age:"); 
int age = int.Parse(Console.ReadLine()); 

bool yes = true; 
bool no = false; 

처음 두 옵션 i 이 있으면 모든 것이 원활하게 진행됩니다. 내가 문 경우 elseConsole.ReadLine();를 작성하는 경우가 도달 할 수없는 코드는 상관없이 내가 입력이 첫 번째 조건을 통해 이동이 종료됩니다 또는 여기

if (selection == 3) 
{ 
    if (age >= 12) 
    { 
     Console.WriteLine("You may enter"); 
    } 
else 
{ 
    { 
     Console.WriteLine("Are you accompanied by an adult? Answer yes or no"); 
     Console.ReadLine(); 

     if (true) 
     { 
      Console.WriteLine("You may pass."); 
     } 
     else if (false) 
     { 
      Console.WriteLine("You are not allowed."); 
     ... 

.

미리 도움을 청하십시오.

+0

'다른 (거짓) {...}'들어갈 기회가 없으니, 다른 뜻인가요? '(true)'가 언제나 비가 오거나 내리면 –

+2

"* 여기에 아무리 입력해도 첫 번째 조건부 *를 통과합니다." 물론 그것은 : Console.ReadLine();의 결과를 무시하고 항상 true 인'if (true)'를 가지게됩니다. –

+0

@DavidArno 바로 가기! 본질적으로'if (true == true)' – EpicKip

답변

2

항상 동일한 답변을 제공하는 true 또는 false를 평가하는 대신 사용자가 변수에 저장하여 사용자가 쓴 내용을 확인해야합니다.

if (age >= 12) 
{ 
    Console.WriteLine("You may enter"); 
} 
else 
{ 
    string response = null; 
    while(response != "yes" || response != "no"){ 
    response = Console.ReadLine("Are you accompanied by an adult? Answer yes or no"); 
    } 

    if (response == "yes") 
    { 
    Console.WriteLine("You may pass."); 
    } 
    //Only other way to get here is if they answered, "no" so don't need to check response 
    else{ 
    Console.WriteLine("You are not allowed."); 
    } 
} 
+1

WriteLine과 ReadLine의 위치를 ​​다시 생각해보고 싶을 수도 있습니다 ... –

+0

고마워, 편집 됨이 –

+0

에 너무나 감사드립니다! 물건을 훨씬 더 명확하게! – owoowow

2

당신은하지 true 또는 false 상수하지만, 실제 사용자의 입력을 확인해야는 bool accompanied 말 :

if (selection == 3) { 
    if (age >= 12) 
    Console.WriteLine("You may enter") 
    else { 
    Console.WriteLine("Are you accompanied by an adult? Answer yes or no"); 

    // Trim() - let's be nice and allow user to leave leading/trailing spaces 
    string input = Console.ReadLine().Trim(); 

    // accompanied if user's input "y" or "yes" (case insensitive) 
    bool accompanied = "yes".Equals(input, StringComparison.OrdinalIgnoreCase) || 
         "y".Equals(input, StringComparison.OrdinalIgnoreCase); 

    if (accompanied) 
     Console.WriteLine("You may pass."); 
    else 
     Console.WriteLine("You are not allowed."); 
    } 
} 
0

당신이 "예/아니오", 당신은 문자열 변수에 저장해야 할 경우, 평가 if이 변수에 따른 조건.

if (selection == 3) 
      { 
      if (age >= 12) 
      { 
       Console.WriteLine("You may enter"); 

      } 
      else 
     { 

      { 
       Console.WriteLine("Are you accompanied by an adult? Answer yes or no"); 
       string res = Console.ReadLine(); 
       if (res == "yes") 

       { 

        Console.WriteLine("You may pass."); 
       } 

       else if (res == "no") 


       { 

        Console.WriteLine("You are not allowed."); 
0

당신은 입력을 읽고,하지만 아무것도 안하고 :

Console.ReadLine(); 

을 그리고 당신은 절대 변하지 않을 것이다 하드 코딩 된 부울 리터럴을 평가하기 위해 노력하고있다 :

if (true) 

대신 실제로 입력되는 내용을 확인하십시오. 예 :

var userInput = Console.ReadLine(); 
if (userInput.Equals("yes", StringComparison.InvariantCultureIgnoreCase)) 
{ 
    Console.WriteLine("You may pass."); 
} 
else 
{ 
    Console.WriteLine("You are not allowed."); 
} 
1

성인의 후속 질문 값을 잊어 버렸습니다.

이 시도 : 당신이 저장하거나 질문에 대한 대답 테스트되지 않습니다 같은

private static void Main(string[] args) 
     { 
      Console.WriteLine(" Enter the number of the film you wish to see :"); 
      int selection = int.Parse(Console.ReadLine()); 
      Console.WriteLine("Enter your age:"); 
      int age = int.Parse(Console.ReadLine()); 

      if (selection == 3) 
      { 
       if (age < 12) 
       { 
        Console.WriteLine("Are you accompanied by an adult? Answer yes or no"); 
        string isAccompanied = Console.ReadLine(); 

        if (isAccompanied.ToUpper().Equals("NO")) 
        { 
         Console.WriteLine("You are not allowed."); 
         return; 
        } 

        Console.WriteLine("You may pass."); 
        return; 
       } 

       Console.WriteLine("You may enter"); 
       return; 
      } 
     } 
0

그것은 보이는 '당신은 성인 동반을?'. readline 메서드의 응답을 저장하고 응답이 예 또는 아니오인지 테스트해야합니다.

IE : 대한 연령 확인하는 변수를 포함하는 파일의 상단을 수정 : -

Console.WriteLine(" Enter the number of the film you wish to see :"); 
    int selection = int.Parse(Console.ReadLine()); 
    Console.WriteLine("Enter your age:"); 
    int age = int.Parse(Console.ReadLine()); 

    bool isUserOldEnough = false; 

을 그리고 같은 당신의 코드를 수정 : - 만약

if (selection == 3) 
    { 
     if (age >= 12) 
     { 
      Console.WriteLine("You may enter"); 
     } 

     else 
     { 
       Console.WriteLine("Are you accompanied by an adult? Answer yes or no"); 

       if (Console.ReadLine().ToLower() == "yes") isUserOldEnough = true; 

       if (isUserOldEnough == true) 
       { 
        Console.WriteLine("You may pass."); 
       } 
       else 
       { 
       Console.WriteLine("You are not allowed."); 
       } 
     } 
    }