黑马程序员-C#基础 4
Convert.ToInt32 可以转换的类型较多;
int.Parse 只能转换数字类型的字符串。
int.TryParse 与 int.Parse 又较为类似,但它不会产生异常,转换成功返回 true,转换失败返回 false。最后一个参数为输出值,如果转换失败,输出值为 0。
class Program
{ static void Main(string[] args)
{
Console.WriteLine("你确定要关机吗");
string s=ShowUI();//定义了一个与返回值相同类型的值 来接收返回值,
if (s == "y")
{ Console.WriteLine("正在关机"); }
else
{ Console.WriteLine("取消关机"); }
Console.ReadKey(); }
public static string ShowUI()//没有返回值的时候用void。一个方法只能有一个返回值,这里的string表示有一个为string类型的返回值。一旦有返回值时必须通过 resutn返回一个值,这个值要与返回值类型相同。
//有 参数的时候 。这个参数用来接收调用者传来的参数
{ string result="";
do
{ result=Console.ReadLine();
if (result!="y"&&result!="n")
{ Console.WriteLine("你输入有问题 ");
}
}
while (result!="y"&&result!="n");//当方法执行完成后,result中储存的就是 用户输入的Y或N return result; } } }
浙公网安备 33010602011771号