[英]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: 您的代码存在一些问题,让我们对其进行分析:
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,在这里您有两个主要入口,但是它们是错误的
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
I think that what you want is: 我认为您想要的是:
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.