2013-10-29 126 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的聲明。你在主函數中聲明瞭你不能做的事。將它移到主體之外,你應該沒問題。