通过命令行从用户获取数据 [英] Getting data from the user via the command line

查看:61
本文介绍了通过命令行从用户获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

推荐答案

有一些您的代码有问题,让我们对其进行分析:

There are some problem with you code, let's analyse them:


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

  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

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

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

我认为您想要的是:


  1. 将静态double Main声明为void Main()

  2. 将您的void main声明为静态void Main(string [] args)

  3. 在新的static void Main调用中(创建矩形之后),它是Main方法(为此必须将其定义为public)

  4. 使用ReadLine而不是Read( )

  5. ReadLine返回一个字符串,以便转换为双精度字,您必须使用lenght = double.Parse(Console.ReadLine())

  6. 最后调用您的r.display()

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

这篇关于通过命令行从用户获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆