2011-03-24 83 views
0

帮助iv'e见过的其他问题,但我与C#初学者错误无效表达术语“其他”

代码:

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 2011-03-24 01:59:16

+0

哦标签!!!! – unknown 2011-03-24 01:59:34

+3

我明白你是初学者,你正在学习。你想要嵌入你的想法的一件事是保持一致的格式。每当你用一个左花括号'{'开始一个新块时,你应该在每一行之后缩进一个额外的级别,并保持所有内容一致,直到你到达一个匹配的右花括号'}'。如果你这样做了,你可以很容易地看到你的代码存在问题。大括号不匹配,你有他们不应该的语句,等等。只要你遵循它,Visual Studio甚至会帮你缩进。 – 2011-03-24 02:03:48

回答

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 2011-03-24 01:57:30

+0

尝试了分号技巧,但它说它预期;在else之后 – unknown 2011-03-24 01:58:15

+5

仅仅因为编译器说它期望的东西并不意味着正确的答案就是放在那里。:) – 2011-03-24 02:02:45

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"); 
}  
} 

编辑:else{if(Y== 'n'){...}}将是很好的重构为

0

我注意到两件事

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"); 
       } 
      } 
     } 
1

使用大括号是一个“代码块”任何事情。当你定义一个class你定义的代码块大括号内是这样的:

大括号内
public class MyClass 
{ 

} 

事情被认为是“在该范围内”。例如:

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声明的情况下,它们是一系列的语句块中,只能方法范围内包含的(你不能使用,例如一类的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." 
     } 
    } 
}