#region
using System;
using System.Collections.Generic;
using System.Text;
#endregionnamespace HelloWord
{
class Program
{
static void Main(string[] args)
{
string s=args[0].ToString();
int number =checked(Convert.ToInt32(args[1]));//为什么这行提示输入正确格式呢。我是在命令行输入2个参数
Console.WriteLine("{0},{1}",s,number);
Console.ReadKey();
} }
}
using System;
using System.Collections.Generic;
using System.Text;
#endregionnamespace HelloWord
{
class Program
{
static void Main(string[] args)
{
string s=args[0].ToString();
int number =checked(Convert.ToInt32(args[1]));//为什么这行提示输入正确格式呢。我是在命令行输入2个参数
Console.WriteLine("{0},{1}",s,number);
Console.ReadKey();
} }
}
string s=args[0].ToString();
int number =checked(Convert.ToInt32(args[1]));//为什么这行提示输入正确格式呢。我是在命令行输入2个参数
Console.WriteLine( "{0},{1} ",s,number);
Console.ReadKey(); --------------------------------------------------
首先args为一个字符数组,checked(Convert.ToInt32(args[1]));
你在这一句强制转当中加了check关键字,如果字符型转换为整形,计算机一般认为都会有数据丢失,所以肯定会报错了
只要把checked关键字去掉,忽略其转换的损失,就不会有问题了
参考写成
int number =Convert.ToInt32(args[1]);
int number =unchecked(Convert.ToInt32(args[1]));
只要参数是有数字就不行
是为什么呢