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#获取“所有用户桌面”的路径

    想用C#得到The All Users Desktop(Public\Desktop)的路径. 原来以为很简单,然而 Environment.GetFolderPath(Environment.Spe ...

  2. dapper 自定义数据库字段和代码中Model字段不一致时候的mapping方法

    namespace YourNamespace { /// <summary> /// Uses the Name value of the ColumnAttribute specifi ...

  3. Pandas简易入门(二)

    目录:     处理缺失数据     制作透视图     删除含空数据的行和列     多行索引     使用apply函数   本节主要介绍如何处理缺失的数据,可以参考原文:https://www. ...

  4. zhuan: ubuntu 安装 apache2

    安装 用  sudo apt-get install apache2 sudo /etc/init.d/apache2 restart 如果发现错误: 以下from:http://cache.baid ...

  5. 使用JS来实现验证码功能

    最近想为自己的Django博客添加验证码功能,本来想使用第三方库来实现的,不过考虑到添加第三方库对性能的影响,以及第三方库是否安全可靠的问题,还是用自己的代码来实现吧.反正用JS来实现验证码功能又不是 ...

  6. Linux进程间通信IPC学习笔记

    linux下的进程通信手段基本上是从Unix平台上的进程通信手段继承而来的.而对Unix发展做出重大贡献的两大主力AT&T的贝尔实验室及BSD(加州大学伯克利分校的伯克利软件发布中心)在进程间 ...

  7. (转)linux性能优化总结

    感谢博客http://sillycat.iteye.com提供的资料 linux性能检查(一)介绍和CPU 通常监控的子系统有: CPU Memory IO Network 应用类型 IO相关,处理大 ...

  8. iOS通过http post上传图片 (转)

    转载自:http://www.cocoachina.com/bbs/read.php?tid=89985 由于iOS无法通过html表单来上传图片,因此想要上传图片,必须实现http请求,而不能像其他 ...

  9. 微软职位内部推荐-Senior SDE for Cloud Platform

    微软近期Open的职位: Microsoft Launched the public cloud service-Azure in China on May 2013, Cosmos is the k ...

  10. Linux操作系统

    Linux操作系统 linux源码分析(三)-start_kernel 2016-10-26 11:01 by 轩脉刃, 146 阅读, 收藏, 编辑 前置:这里使用的linux版本是4.8,x86体 ...