Getting data from the user via the command line

2019-09-04 07:06发布

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? 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.

2条回答
【Aperson】
2楼-- · 2019-09-04 07:36

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."

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

And yes, having two main methods is confusing and senseless.

查看更多
仙女界的扛把子
3楼-- · 2019-09-04 07:39

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

  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

  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
  4. your static double Main doesn't return a double

I think that what you want is:

  1. declare the static double Main as void Main()
  2. declare your void Main as static 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)
  4. use ReadLine instead of Read()
  5. ReadLine return a string so to transform that in a double you have to use lenght = double.Parse(Console.ReadLine())
  6. finally call your 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();
    }
}
查看更多
登录 后发表回答