在C#中无法将“void”隐式转换为“bool”怎样解决

来源:百度知道 编辑:UC知道 时间:2024/05/30 22:05:07
这里有错误:
if ( Console.WriteLine("请输入您要出行的月份:1-12"))
month = int.Parse(Console.ReadLine());
我向给month赋成一个int型的只,然后系统就提示出错了!

把if去掉即可:
Console.WriteLine("请输入您要出行的月份:1-12");
int month = int.Parse(Console.ReadLine());

原因:Console.WriteLine("");方法返回值为void,在这里不知加if为何用意。

另外,类型转换。需做处理,以防转换时出现异常,至程序崩溃。

换成try
{
Console.WriteLine("请输入您要出行的月份:1-12")
month = int.Parse(Console.ReadLine());
}
catch
{}
吧,和你想要的效果一样

这个错误肯定是你有个地方用一个bool类型的值去调用了无返回值的方法,所以会报错.
解决方法:1、直接调用方法,不能用任何类型的值去调用。
2、修改方法使之成为一个有返回值并且返回值是bool类型的方法。

第二句改成这个 month = convert.toint32(Console.ReadLine()) 前面再加些判断

Console.WriteLine("请输入您要出行的月份:1-12")

whlie(!int.TryParse(Console.ReadLine(),out month )||month<1||month>12)
{
Console.WriteLine("请输入正确数字 ");
}
Console.WriteLine("您要出行的月份:{0}",month.ToString());