【发布时间】: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