C# TryParse

https://www.cnblogs.com/dream-game/p/5532506.html

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace tryparse

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine("请输入圆的半径");

int r; //定义半径变量

bool x = int.TryParse(Console.ReadLine(), out r);

/*判断用户的输入是否为整型的数据,如果在此处输入的不是整形数值

此处会返回false,并赋值给x,此时r的值已经变成了0,如果此处用户输入了

有效的数值,此处会返回truer会保持用户输入的值待用*/

if (x)//通过判断返回值可以判断输入是否有效。

{

const double PI = 3.14;//如果有效继续运行

double circleArea = r * r * PI;

Console.WriteLine("该圆的面积为:{0}", circleArea);

//break;

Console.ReadKey();

 

}

 

else

{

Console.WriteLine("请输入整数类型的半径");//如果无效提示用户重新输入

Console.ReadKey();

}

 

 

}

 

}

}

posted @ 2020-06-16 17:28  司徒无名  阅读(461)  评论(0编辑  收藏  举报