2013-10-29 71 views
0

我是C#的新手,在我的教科书中,它要求我向Program.cs类的主要方法中添加一些代码,但没有告诉我如何。我是一名初学者,所以我只是在寻找基础知识,而当我出发时,我会拿起更先进的课程,所以请让你的解释彻底,但下降到第1级。以下是我提供的代码。它不断地为我提供了错误的<了解C#代码机制

这里是下面的代码:我应该向公众静态声音TestIfElse方法添加到Program.cs文件类:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace ifelse_Statement 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      TestIfElse(10); 
      public static void TestIfElse(int n) 
      { 
       if (n < 10) 
       { 
           Console.WriteLine(“n is less than 10”); 
       } 
       else if (n < 20) 
       { 
           Console.WriteLine(“n is less than 20”); 
       } 
       else if (n < 30) 
       { 
           Console.WriteLine(“n is less than 30”); 
       } 
       else 
       { 
           Console.WriteLine(“n is greater than or equal to 30”); 
       } 
      } 
     } 
    } 
} 

回答

3

你的错误很简单 - 你可以”在C#中嵌套函数。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace ifelse_Statement 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      TestIfElse(10); 
     } 
     public static void TestIfElse(int n) 
     { 
      if (n < 10) 
      { 
       Console.WriteLine(“n is less than 10”); 
      } 
      else if (n < 20) 
      { 
       Console.WriteLine(“n is less than 20”); 
      } 
      else if (n < 30) 
      { 
       Console.WriteLine(“n is less than 30”); 
      } 
      else 
      { 
       Console.WriteLine(“n is greater than or equal to 30”); 
      } 
     } 
    } 
} 
0

您应该将TestIfElse方法的主体移到Main方法的主体之外。这样他们都被认为是课堂的方法,并且会按需要工作。

此外,在你的逻辑中,你需要检查范围,即小于20的数字也小于30,所以你可能想要检查一个数字是否在10到20之间,或者20到30等等。

static void Main(string[] args) 
    { 
     TestIfElse(10); 
    } 

public static void TestIfElse(int n) 
{ 
    if (n < 10) 
    { 
     Console.WriteLine(“n is less than 10”); 
    } 
    else if (n < 20) 
    { 
     Console.WriteLine(“n is less than 20”); 
    } 
    else if (n < 30) 
    { 
     Console.WriteLine(“n is less than 30”); 
    } 
    else 
    { 
     Console.WriteLine(“n is greater than or equal to 30”); 
    } 
} 
0

ClassHere是一类

public class MyClass // this is the declaration of the class 
{ 
    // this is a property, it is accessible by things outside of this class. 
    public static string MyProperty { get; set; } ; 
    private static string _myField; // this is a ['private] field, it is intended to store the state of the object. it cannot be accessed from outside of this class 

    static void Main(string[] args) 
    { 
     // this is the method that gets run first so it make all of your initial calls 
    } 

    public static void TestIfElse(int n) 
    { 
     // this is another method (taught as module, operation, action, or subroutine in schools) 
     // it has return type of void which is more or less "nothing". This type of behavior simply does 
     // something but doesn't return a value 
    } 

    public static bool IsNotPrime(int input) 
    { 
     // this is an actual function in that will return a single value whether its a primitive value or an 
     // object. Whatever it is, there's ONE. The point is that a call to this function is now synonymous with 
     // the value it returns. So for example, if this method was real, it is equivalent to 'true' so you could 
     // actually say if(IsNotPrime(8)){ // do things } 
     return input % 2 == 0; 
    } 
} 

的基本结构,你必须保持这些方法分开。类可以有许多成员(字段,属性,方法等),一个方法可以调用另一个方法,但方法不能包含另一个。所以当你学习认识这些时,如果你看到公共或静态的关键字,你应该认为这些需要成为他们自己的实体,而不是其他类成员。

例如,除类声明本身之外,privateprivate static(或任何其他访问修饰符)类型和标识之前,你看到声明一个类的方法时的第一件事情,所以不要试图把任何东西就像那个成员的内部那样。

你在代码中遇到的问题主要是关于TestIfElse的声明。你在主函数中声明了你不能做的事。将它移到主体之外,你应该没问题。