Area of a Circle

Description:

Complete the function circleArea so that it will return the area of a circle with the given radius. Round the returned number to two decimal places (except for Haskell). If the radius is not positive or not a number, return false.

Example:

Kata.CalculateAreaOfCircle("-123"); //throws ArgumentException
Kata.CalculateAreaOfCircle("0"); //throws ArgumentException
Kata.CalculateAreaOfCircle("43.2673"); //return 5881.25
Kata.CalculateAreaOfCircle("68"); //return 14526.72
Kata.CalculateAreaOfCircle("number"); //throws ArgumentException
using System;

public static class Kata
{
public static double CalculateAreaOfCircle(string radius)
{
//Solve!
try
{
//if (radius.IndexOf(',') != -1)
if (radius.Contains(","))
{
throw new ArgumentException();
}
double r = Convert.ToDouble(radius);
if (r <= )
{
throw new ArgumentException();
}
else
{
return Convert.ToDouble((Math.PI * Math.Pow(r, )).ToString("f2"));
}
}
catch
{
throw new ArgumentException();
}
}
}

其他人的解法:

使用Math.Round以及out参数  double的TryParse

using System;

public static class Kata
{
public static double CalculateAreaOfCircle(string radius)
{
double number; if (radius.Contains(",") || !double.TryParse(radius, out number) || double.TryParse(radius, out number) && number < )
{
throw new ArgumentException();
} return Math.Round(number * number * Math.PI, ); }
}

Area of a Circle的更多相关文章

  1. 创建一个接口Shape,其中有抽象方法area,类Circle 、Rectangle实现area方法计算其面积并返回。又有Star实现Shape的area方法,其返回值是0,Star类另有一返回值boolean型方法isStar;在main方法里创建一个Vector,根据随机数的不同向其中加入Shape的不同子类对象(如是1,生成Circle对象;如是2,生成Rectangle对象;如是3,生成S

    题目补充: 创建一个接口Shape,其中有抽象方法area,类Circle .Rectangle实现area方法计算其面积并返回. 又有Star实现Shape的area方法,其返回值是0,Star类另 ...

  2. circle area

    circle area Github 链接:传送门 本次作业要求 Create a program that asks for the radius of a circle and prints th ...

  3. 第一次课堂作业之Circle

    1.问题描述: Create a program that asks for the radius of a circle and prints the area of that circle, us ...

  4. 实现一个类 RequireJS 的模块加载器 (二)

    2017 新年好 ! 新年第一天对我来说真是悲伤 ,早上兴冲冲地爬起来背着书包跑去实验室,结果今天大家都休息 .回宿舍的时候发现书包湿了,原来盒子装的牛奶盖子松了,泼了一书包,电脑风扇口和USB口都进 ...

  5. es6

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 跟vczh看实例学编译原理——零:序言

    在<如何设计一门语言>里面,我讲了一些语言方面的东西,还有痛快的喷了一些XX粉什么的.不过单纯讲这个也是很无聊的,所以我开了这个<跟vczh看实例学编译原理>系列,意在科普一些 ...

  7. C#设计模式系列:开闭原则(Open Close Principle)

    1.开闭原则简介 开闭原则对扩展开放,对修改关闭,开闭原则是面向对象设计中可复用设计的基石. 2.开闭原则的实现 实现开闭原则的关键就在于抽象,把系统的所有可能的行为抽象成一个抽象底层,这个抽象底层规 ...

  8. Javascript闭包深入解析及实现方法

    1.什么是闭包 闭包,官方对闭包的解释是:一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分.闭包的特点:1. 作为一个函数变量的一个引用,当函数返回时 ...

  9. 瘋子C++笔记

    瘋耔C++笔记 欢迎关注瘋耔新浪微博:http://weibo.com/cpjphone 参考:C++程序设计(谭浩强) 参考:http://c.biancheng.net/cpp/biancheng ...

随机推荐

  1. C#反编译工具 ILSPY-x64可动态调试-君临汉化版

    程序基于著名的ILSpy version 2.1.0.1603 汉化,并增加x64下debugging功能;初衷是网上只有一版是原作者留下的x86版本,实在不想在虚拟机里调试,只有自己动手弄一份x64 ...

  2. C#语言的Image和byte数组的互相转换

    /// <summary> /// 字节数组转换为图片 /// </summary> /// <param name="buffer">字节数组 ...

  3. node.js:怎样同时执行多条SQLs,且只有一个回调

    本文主要介绍开源node.js库mysql-queries,其可以同时执行多条SQLs,且只有一个回调.同时抛砖引玉,与大家交流node.js开发经验. node.js很大的特点就是事件驱动.非阻塞和 ...

  4. 同时存在两个或多个homestead 虚拟box

    开发中发现,不同版本的homestead 里面的环境各不相同,里面的node,npm等版本都不一致,如果需要添加 不同版本的homestead同时存在可以按照以下办法处理. tips: 提供可以离线下 ...

  5. C# - write values to configuration file

    using System.Configuration;System.Configuration.Configuration config = ConfigurationManager.OpenExeC ...

  6. Object-C - 类的定义

    http://www.cnblogs.com/zhangweia/archive/2011/11/01/2231549.html 1. 文件分为.h:定义接口,及其属性,方法说明. .m :是实现类. ...

  7. OWIN and Katana - 1

    翻译自 http://www.asp.net/aspnet/overview/owin-and-katana/an-overview-of-project-katana 十多年来,基于ASP.NET框 ...

  8. spring-cloud-hystrix熔断

    依赖pom <dependencyManagement> <dependencies> <dependency> <groupId>org.spring ...

  9. pdf转chm的实现方法

    相比pdf, CHM电子书在Windows系统下不需要安装额外的浏览器即可进行阅读,其内容是基于浏览器的风格,更容易被用户所接受.而且, 具有更强大的功能配置,比如可提供强大的全文搜索.索引.书签等的 ...

  10. V9任何页面GET调用内容分页的说明

    如标题,很多人想要在网站首页或其他的页面实现分页效果,说明如下: 一般特殊页面实现分页是通过GET语句的(论坛很多牛人用修改PHPCMS系统函数来实现,个人不推荐,因为你改了系统文件,不利于官方下一步 ...