c# - "The namespace ' 控制台 ' already contains a definition for ' 圆圈 '"

标签 c# class methods system geometry

我正在为一个学校项目编写一些代码,其中我们将有一个类定义一个圆和一个从中绘制的方法,我的代码到目前为止返回“命名空间‘console’已经包含了‘的定义’ Circle' 这里是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace console
{
    class Program
    {

        static void Main(string[] args)
        {

            Console.Write("What is the circle’s radius: ");
            double radius = Convert.ToDouble(Console.ReadLine());
            Circle ans = new Circle(radius);


            Console.WriteLine("The area of the circle is " + ans.getArea);
            Console.WriteLine("The Diameter of the circle is " + ans.getDiameter);
            Console.WriteLine("The Circumference of the circle is " + ans.getCircumference);

            Console.Write("Enter any character to quit program. ");
            double stop = Console.Read();

        }
    }
}

这是方法和类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace console
{
    class Circle
    {

 public double radius;
            public const double PI = 3.14159;
            public Circle(double r)
            {
            radius = r;
        }
        public Circle()
        {
            radius = 0.0;
        }

        public double getDiameter
        {
            get
            {
                return radius * 2;
            }

        }


        public double getArea
        {
            get
            {
                return PI * radius * radius;
            }

        }
        public double getCircumference
        {
            get
            {
                return 2 * PI * radius;
            }

        }
        // property Radius 
        public double Radius
        {
            get
            {
                return radius;
            }

            set
            {
                // ensure non-negative radius value 
                if (value >= 0)
                    radius = value;
            }
        }
    }
}

关于这个的最后一个问题,现在我有了工作代码,set 和 get 应该不允许负输入。但是,当它运行时,负输入仍然返回结果,我错过了什么导致仍然计算负值?

最佳答案

(编译器输出信息是你的 friend :一定要学会理解它们。)

在同一个命名空间中不能有两个同名的类。

Circleconsole 命名空间中出现了两次。

只需重命名其中一个即可。如果我是你,我会更改第一个,因为它并不是真正的圆形建模。然而,第二类是。

关于c# - "The namespace ' 控制台 ' already contains a definition for ' 圆圈 '",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22873740/

相关文章:

c# - 执行其他功能时,SheetChange 事件未获取事件 Excel 单元格的更改

JQuery 用编号类包裹 div

c++ - 使用 C++ 时出现错误 : no matching function for call to ,

c++ - 未使用依赖类型的“预期主表达式”错误

使用字符串的 Java 方法

c# - ASP.net 在尝试将文件上传到我的 Windows Server 2008 R2 Web 服务器时出现错误 "Access to the path is denied."

c# - 是否可以在两个项目中声明一个部分类

c# - 如何在 WEB API 中获取 WWWForm

java - Android 适配器未更新

javascript - 获取方法外部的变量(对象)