簡體   English   中英

錯誤:程序有多個入口點

[英]Error: Program has more than one entry point

我想讓用戶輸入lengthwidth ,以便我們可以計算矩形的面積。 我有兩個類,它們是ProgramRectangle 問題是當我嘗試運行此代碼時出現錯誤:

程序有多個入口點

如何解決這個問題?

這是我的代碼:

using System;
using System.Collections.Generic;

namespace rec_oop_20211025_E3
{
    class Program
    {
        static void Main(string[] args)
        {
            
            List<Rectangle> recs = new List<Rectangle>(); 

            ConsoleColor errColor = ConsoleColor.DarkRed; // to make the error part red in color
            ConsoleColor defColor = Console.BackgroundColor; // defColor is default color
            start: // use start to run many times
            Console.Write("Input sides (eg. 5,6): ");
            string line = Console.ReadLine();
            Rectangle rec = new Rectangle();
            try
            {
                rec.SetSides(line, ",");
                Console.WriteLine($"{rec.GetInfo()}");
                recs.Add(rec);
            }
            catch(Exception ex)
            {
                Console.BackgroundColor = errColor;
                Console.WriteLine($"Error > {ex.Message}");
                Console.BackgroundColor = defColor;
            }
            Console.WriteLine("\nEsc to stop, any key for another rectangle...");
            char key = Console.ReadKey(true).KeyChar; // with start we need this
            if (key != (char)ConsoleKey.Escape)
                goto start;

            Console.WriteLine();
            Console.BackgroundColor = ConsoleColor.DarkGreen;
            double total = 0;
            foreach(var r in recs)
            {
                total += r.GetArea();
                Console.WriteLine(r.GetInfo());
            }

            Console.BackgroundColor = ConsoleColor.DarkBlue;
            Console.WriteLine($"\nTotal = {total:n3}");
            Console.BackgroundColor = defColor;
        }
    }

public class Rectangle
    {
        //static methods
        public static void CheckSides(double width, double length)
        {
            if (width < 0 || length < 0)
                throw new Exception($"Rectangle sides( {width}, {length}) are invalid.");
        }
        public static  void TryParse(string data, string delimiter, out double width, out double length)
        {
            try
            {
                string[] arr = data.Split(delimiter);
                width = double.Parse(arr[0]);
                length = double.Parse(arr[1]);
            }
            catch (Exception)
            {
                throw new Exception($"Given data \"{data}\" are invalid.");
            }
        }

        //intance fields
        private double wd;
        private double lng;

        //instance methods
        public void SetSides(string data, string delimiter)
        {
            double width, length;
            Rectangle.TryParse(data, delimiter, out width, out length);
            Rectangle.CheckSides(width, length);
            wd = width;
            lng = length;
        }
        public string GetInfo() => $"width={wd}, length={lng} > area={GetArea():n3}";
        
        //public double GetArea() { return wd * lng; }
        public double GetArea() => wd * lng;
    }
}
}

您可以查看有關此錯誤的官方文檔

要解決此錯誤,您可以刪除代碼中的所有 Main 方法,一個除外,也可以使用 StartupObject 編譯器選項指定要使用的 Main 方法。

您也可以使用命令行上的/main開關來指示包含適當入口點的類型。

這就是這個 - 錯誤非常具有描述性。 嘗試搜索( Ctrl + shift + FMain( (僅此文本main然后 1 個圓括號)-您應該找到另一個 function 隱藏在定義void Main的某個地方-這不可能。只有 1 static void Main()可能存在.刪除另一個,現在你的應用程序可以編譯了。如果你有其他類沒關系 - 唯一認為重要的是名為Main的 function 可能只有一個

PS如果我的回答還不夠——你還有一些問題——請對你的代碼做一個最低限度的克隆,並把它變成 Github repo——這樣我們就可以看看代碼

您的代碼末尾有太多}括號。 刪除最后一個,您的問題應該得到解決:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM