简体   繁体   English

通过命令行从用户获取数据

[英]Getting data from the user via the command line

I want to have someone input a value for length and width in my code, here is what I got so far: 我想要有人在我的代码中输入长度和宽度的值,这是到目前为止我得到的:

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();
        }
    }
}

Is trying to use two Main methods the wrong way to approach this? 尝试使用两种Main方法是错误的方式吗? This is code I was copying from http://www.tutorialspoint.com/csharp/csharp_basic_syntax.htm I'm trying to modify it around to just to get more experience with this programming language. 这是我从http://www.tutorialspoint.com/csharp/csharp_basic_syntax.htm复制的代码,我正在尝试对其进行修改,以期获得使用该编程语言的更多经验。

There are some problem with you code, let's analyse them: 您的代码存在一些问题,让我们对其进行分析:

  1. a program must have a unique entry point and it must be a declared as static void, here you have two main but they are wrong 程序必须具有唯一的入口点,并且必须声明为static void,在这里您有两个主要入口,但是它们是错误的

  2. you in your static Main the one in the rectangle class you can't reference the variables length e width because they're not declared as static 您在静态Main中处于矩形类中,因此您不能引用变量length和width,因为它们没有被声明为static

  3. console.Read() return an int that represent a character so using if the user inputs 1 you may have a different value in your length variable console.Read()返回一个表示字符的int值,因此如果用户输入1,则可以使用长度变量中的另一个值
  4. your static double Main doesn't return a double 您的静态double主键不会返回double

I think that what you want is: 我认为您想要的是:

  1. declare the static double Main as void Main() 将静态double Main声明为void Main()
  2. declare your void Main as static void Main(string[] args) 将您的void Main声明为静态void Main(string [] args)
  3. in your new static void Main call (after you create the rectangle) it's Main method (to do that you have to define it as public) 在新的static void Main调用中(创建矩形之后),它是Main方法(为此,您必须将其定义为public)
  4. use ReadLine instead of Read() 使用ReadLine代替Read()
  5. ReadLine return a string so to transform that in a double you have to use lenght = double.Parse(Console.ReadLine()) ReadLine返回一个字符串,以便转换为双精度字,您必须使用lenght = double.Parse(Console.ReadLine())
  6. finally call your r.display() 最后调用您的r.display()

This is a working code that do what you want. 这是执行您想要的工作代码。 Note before copy pasting, since you're trying and lear read the steps and try to fix it without looking at the code 请注意在复制粘贴之前,因为您正在尝试并且希望阅读这些步骤并尝试在不查看代码的情况下对其进行修复

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();
    }
}

In this cases you'll have to tell the compiles wich is the class with the entry point. 在这种情况下,您必须告诉编译器是带有入口点的类。

"If your compilation includes more than one type with a Main method, you can specify which type contains the Main method that you want to use as the entry point into the program." “如果您的编译通过Main方法包含多个类型,则可以指定哪种类型包含要用作程序入口点的Main方法。”

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

And yes, having two main methods is confusing and senseless. 是的,拥有两种主要方法既令人困惑又毫无意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM