2015-04-25 52 views
-2

我想要做的是在學習C#時創建一個項目,併爲每個Euler問題在該項目中創建一個新類。類文件中的入口點 - 超過1個入口點

所以我有一個基地的Program.cs文件,它有它的入口點。

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

namespace Euler 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
     } 
    } 
} 

但是我正確定義了我在自己的類中的入口點,並且有一個錯誤清楚地顯示我有多個入口點。

如何正確設置我的輸入?

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

namespace Euler 
{ 
    //Problem 1 
    //If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. 
    //Find the sum of all the multiples of 3 or 5 below 1000. 

    class Problem1 
    { 
     public Problem1(int args) 
     { 
      int num = 0; 
      int limit = 1000; 
      int sum = 0; 
      while (num < limit) 
      { 
       if ((num % 3 ==0)|| (num % 5 == 0)) 
       { 
        sum = sum + num; 
        num++; 
       } 
       else 
       { 
        num++; 
       } 
      } 
      Console.WriteLine(sum); 
     } 
    } 


} 

我知道這是一個新手,但它似乎並不明顯。

Error 1 Program 'c:\Users\Sayth\Documents\Visual Studio 2013\Projects\Euler\Euler\obj\Debug\Euler.exe' has more than one entry point defined: 'Euler.Program.Main(string[])'. Compile with /main to specify the type that contains the entry point. c:\users\sayth\documents\visual studio 2013\Projects\Euler\Euler\Program.cs 11 21 Euler 
Error 2 Program 'c:\Users\Sayth\Documents\Visual Studio 2013\Projects\Euler\Euler\obj\Debug\Euler.exe' has more than one entry point defined: 'Euler.Problem1.Main(string[])'. Compile with /main to specify the type that contains the entry point. c:\users\sayth\documents\visual studio 2013\Projects\Euler\Euler\Problem1.cs 15 21 Euler 

編輯我已經解決了入口點錯誤,下面的代碼並不但都沒有涉及這個問題的其他原因的工作。

class Problem1 
    { 
     public int Sum53(int args) 
     { 
      int num = 0; 
      int limit = 1000; 
      int sum = 0; 
      while (num < limit) 
      { 
       if ((num % 3 == 0) || (num % 5 == 0)) 
       { 
        sum = sum + num; 
        num++; 
       } 
       else 
       { 
        num++; 
       } 
      } 
      return sum; 
     } 

     public string myValue(string args) 
     { 
      Console.WriteLine("This is the total :" +); 
     } 
    } 
} 
+2

我在那裏只看到一個入口點。如果您確實有多個,請確保在您的項目設置中設置啓動對象。 – BradleyDotNET

+4

顯然沒有足夠的信息來診斷......我敢打賭,在整個項目中搜索「主」會揭示問題。 –

+0

@AlexeiLevenkov不,你沒有任何其他建議嗎? – sayth

回答

1

您只能在程序中有一個入口點(除非您告訴編譯器否則使用哪一個入口點)。但是那個入口點可以調用任何你想要的。因此,如果您想要使用多個不同的輸入函數進行試驗,可以簡單地取消註釋您想要運行的功能。

static void Main(string[] args) //You can't have any other functions with this signature in your project 
{ 
    Function1(args); 
    //Function3(args); 
    //Function2(args); 
} 

static void Function1(string[] args) 
{ 
} 

static void Function2(string[] args) 
{ 
} 

static void Function3(string[] args) 
{ 
} 
+0

您可能的重複*可以*具有多個具有相同'Main'簽名的函數,但是如果這樣做,則必須使用編譯器開關來指示框架應鏈接到哪個函數。這可能是在問題的基本級別之上,但是... – Claies

+0

@Claies是的,我暗示了那個編譯器開關,但是我沒有舉一個例子,因爲我以前從未使用它。隨意編輯它到我的問題或張貼您自己的答案。它可能高於問題海報的水平,但堆棧溢出的問題不僅是爲了海報的益處:它們適用於所有未來出現類似問題的其他人。 – mason