2011-03-24 5 views
0

도움말오류 잘못된 표현 용어는 '다른'

코드 : 당신은 다른 후 세미콜론이 없어야

namespace Input_Program 
{ 
    class Program 
    { 
     static void Main() 
     { 
      char Y = Console.ReadKey().KeyChar; 

      Console.WriteLine("Welcome to my bool program!"); 
      Console.WriteLine("Input a NON capital y or n when told to."); 

      if(Y == 'y') 
      { 
       Console.WriteLine("Thank you,Please wait....."); 
       Console.WriteLine("You input Y"); 

       else; 
       { 
        if(Y == 'n') 
        { 
         Console.WriteLine("You input N"); 
      } 
     } 
    } 
      Console.ReadLine(); 
} 
+0

그는 무엇을 변경 했습니까? – unknown

+0

아, 태그 !!!! – unknown

+3

나는 초심자이며 배우고 있음을 이해합니다. 당신이 염두에두고 싶은 한 가지는 일관된 형식을 유지하는 것입니다. 왼쪽 중괄호'{'를 사용하여 새 블록을 시작할 때마다 모든 추가 행을 들여 씁니다. 일치하는 오른쪽 중괄호 {}'에 도달 할 때까지 모든 행을 계속 유지해야합니다. 그렇게 했더라도 코드에 문제가 있음을 쉽게 알 수있었습니다. 중괄호는 일치하지 않으며, 문장을 포함하지 않아야합니다. Visual Studio는 문장을 따라 가면 들여 쓰기를 도와줍니다. –

답변

2

, 너는 다른 사람 앞에서 닫는 멍청이를 놓치고있어.

namespace Input_Program 
{ 
    class Program 
    { 
     static void Main() 
     { 
      char Y = Console.ReadKey().KeyChar; 
      Console.WriteLine("Welcome to my bool program!"); 
      Console.WriteLine("Input a NON capital y or n when told to."); 
      if(Y == 'y') 
      { 
       Console.WriteLine("Thank you,Please wait....."); 
       Console.WriteLine("You input Y"); 
      } 
      else 
      { 
       if(Y == 'n') 
       { 
        Console.WriteLine("You input N"); 
       } 
      } 
      Console.ReadLine(); 
     } 
    } 
}   
+0

감사합니다. 좋은 그림 – unknown

+0

세미콜론 트릭을 시도했지만 예상대로입니다. 그 후 else – unknown

+5

컴파일러가 말하기를 뭔가가 올바른 대답을 의미하는 것은 아니라는 것을 예상했기 때문입니다.:) –

1

당신은 (단지 else 전) if에 의해 제어되는 두 문장 후 닫는 중괄호 }이 필요합니다. 또한 한 번 수행하면 else 뒤에 세미콜론을 제거하십시오.

0
{ 
    char Y = Console.ReadKey().KeyChar; 
    Console.WriteLine("Welcome to my bool program!") 
    Console.WriteLine("Input a NON capital y or n when told to."); 

    if(Y == 'y') 
    { 
     Console.WriteLine("Thank you,Please wait....."); 
     Console.WriteLine("You input Y");  
    }else 
    { 
     if(Y == 'n') 
     { 
      Console.WriteLine("You input N"); 
     } 
    } 

    Console.ReadLine(); 
} 

하여 들여 쓰기를 일치하는 것은 당신이 필요로/중괄호를 닫을 필요가 없습니다 당신이 다른 후 세미콜론을 넣지 마십시오 위치를 확인하는 데 도움이됩니다.

구조체를 if/else 또는 두 개의 if와 동일하게 변경하여 동일한 결과를 얻을 수도 있습니다.

{ 
    char Y = Console.ReadKey().KeyChar; 
    Console.WriteLine("Welcome to my bool program!") 
    Console.WriteLine("Input a NON capital y or n when told to."); 

    if(Y == 'y') 
    { 
     Console.WriteLine("Thank you,Please wait....."); 
     Console.WriteLine("You input Y");  
    } 

    if(Y == 'n') 
    { 
     Console.WriteLine("You input N"); 
    } 

    Console.ReadLine(); 
} 
1

else은 뒤에 세미콜론이없는, 당신은 당신의 중괄호를 닫아야합니다. 당신의 구문을 다시 실행 :

if(Y == 'y'){ 
Console.WriteLine("Thank you,Please wait....."); 
Console.WriteLine("You input Y"); 
} 
else{ 
if(Y == 'n'){ 
    Console.WriteLine("You input N"); 
}  
} 

편집 : 나는 두 가지

if(Y == 'y') 
     { 
      Console.WriteLine("Thank you,Please wait....."); 
      Console.WriteLine("You input Y"); 
      // you need to close the if with a } on this line  
      else; //no semicolon after else 
      { 
       if(Y == 'n') 
       { 
        Console.WriteLine("You input N"); 
       } 
      } 
     } 
0

else if(Y=='n'){...}else{if(Y== 'n'){...}} 리팩토링 좋은 것입니다. 당신이 class를 정의 할 때 당신은 다음과 같이 중괄호 안에 코드 블록을 정의 중괄호 내

public class MyClass 
{ 

} 

것 "이 그 범위에"로 간주됩니다. 예를 들어 : IF 문장의 경우

public class MyClass 
{ 
    // This method is in the scope of MyClass 
    public void MyMethod() 
    { 
     // This variable is in the scope of MyMethod. 
     // It is only accessible from within this method because that is where 
     // it is defined. 
     string myString = "Hello Method."; 
    } 

    // This variable is in the scope of MyClass. 
    // It is accessible within MyClass, including all methods that are also in scope 
    // of MyClass 
    public string myGlobalString = "Hello Global."; 
} 

, 그들은 블록 일련의 문장입니다 만 (당신은 예를 들어 클래스에 IF 문을 사용할 수없는) 방법의 범위에 포함 할 수 있습니다. 코드는 명령문에서 제공 한 경로 중 하나를 이동할 수 있습니다. 예 :

public class MyClass 
{ 
    public void MyMethod(string myString) 
    { 
     if (myString == "Hello") 
     { 
      // Read like English. "If variable myString equals the value 'Hello', then do this code within this block." 
     } 
     else if (myString == "Goodbye") 
     { 
      // "...or else if variable myString equals the value 'Goodbye', then do this code within this block instead. 
     } 
     else if (myString == "Good Morning") 
     { 
      // "...or else if variable myString equals the value 'Good Morning', then do this code within this block instead. 
     } 
     else 
     { 
      // "...or else do this code if variable myString does not match any of the above code statements. 
     } 

     // You are not required to include "else" or even "else if". You could just do a single IF statement like this: 
     if (myString == "hi) 
     { 
      // "If variable myString equals the value 'hi' then do this, otherwise do nothing." 
     } 
    } 
} 
1

는 "코드 블록"입니다 중괄호를 사용 아무것도 발견