【问题标题】:Getting data from the user via the command line通过命令行从用户那里获取数据
【发布时间】:2013-08-24 21:06:55
【问题描述】:

我想让某人在我的代码中输入一个长度和宽度的值,这是我目前得到的:

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

namespace ConsoleApplication2
{
    class Rectangle
    {
        double length;
        double width;
        double a;

        static double Main(string[] args)
        {
            length = Console.Read();
            width = Console.Read();
        }

        public void Acceptdetails()
        {

        }

        public double GetArea()
        {
            return length * width;
        }

        public void Display()
        {
            Console.WriteLine("Length: {0}", length);
            Console.WriteLine("Width: {0}", width);
            Console.WriteLine("Area: {0}", GetArea());
        }
    }

    class ExecuteRectangle
    {
        public void Main()
        {
            Rectangle r = new Rectangle();

            r.Display();
            Console.ReadLine();
        }
    }
}

尝试使用两个Main 方法是错误的方法吗?这是我从 http://www.tutorialspoint.com/csharp/csharp_basic_syntax.htm 复制的代码,我正在尝试对其进行修改,以获得更多使用这种编程语言的经验。

【问题讨论】:

  • Console.Read 返回 1 char,而不是您期望的整数(或双精度)值。阅读一些关于Console.ReadLine、double.Parse的文档
  • 与您的示例不同,该类是静态的,因此请制作长度、宽度等静态变量。并且没有理由拥有多个名为 main 的方法。将 main 更改为 static int Main(string [] args) 并将 return 0 放在底部。

标签: c# command-line


【解决方案1】:

你的代码有问题,我们来分析一下:

  1. 一个程序必须有一个唯一的入口点并且它必须被声明为静态 void,这里你有两个 main 但它们是错误的

  2. 你在你的静态 Main 中的一个矩形类中你不能引用变量 length e width 因为它们没有被声明为静态

  3. console.Read() 返回一个表示字符的 int,因此如果用户输入 1,您的长度变量中可能会有不同的值
  4. 您的静态双精度 Main 不返回双精度

我认为你想要的是:

  1. 将静态 double Main 声明为 void Main()
  2. 将您的 void Main 声明为 static void Main(string[] args)
  3. 在您的新静态 void Main 调用中(在您创建矩形之后)它是 Main 方法(要做到这一点,您必须将其定义为公共)
  4. 使用 ReadLine 代替 Read()
  5. ReadLine 返回一个字符串,因此要将其转换为双精度,您必须使用 lenght = double.Parse(Console.ReadLine())
  6. 最后调用你的 r.display()

这是一个可以做你想做的工作的代码。 复制粘贴前请注意,因为您正在尝试并阅读步骤并尝试在不查看代码的情况下修复它

class Rectangle
{
    double length;
    double width;
    double a;
    public void GetValues()
    {
        length = double.Parse(Console.ReadLine());
        width = double.Parse(Console.ReadLine());
    }
    public void Acceptdetails()
    {

    }
    public double GetArea()
    {
        return length * width;
    }
    public void Display()
    {
        Console.WriteLine("Length: {0}", length);
        Console.WriteLine("Width: {0}", width);
        Console.WriteLine("Area: {0}", GetArea());
    }

}
class ExecuteRectangle
{
    public static void Main(string[] args)
    {

        Rectangle r = new Rectangle();
        r.GetValues();
        r.Display();
        Console.ReadLine();
    }
}

【讨论】:

  • 查看我的代码,我将其中一个 Main 重命名为更合适的名称 (GetValues)。您的程序必须有一个定义为 public static void Main(string[] args) 的入口点,入口点是程序执行时调用的第一个方法。
【解决方案2】:

在这种情况下,您必须告诉编译器哪个是具有入口点的类。

“如果您的编译包含多个具有 Main 方法的类型,您可以指定哪个类型包含要用作程序入口点的 Main 方法。”

http://msdn.microsoft.com/en-us/library/x3eht538.aspx

是的,有两种主要方法令人困惑且毫无意义。

【讨论】:

  • 我已经指定了要使用的 Main 方法。我仍然收到错误消息。
  • 'ConsoleApplication2.Rectangle' 没有合适的静态 Main 方法
  • Main 必须返回 void,没有 double。
  • 'ConsoleApplication2.Rectangle.Main(string[])' 作为入口点的签名错误
  • 必须是:static void Main(string[] args)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多