看代码:
using System;
using System.Windows.Forms;class yuan
{
public static void Main()
{
string strfs=Console.ReadLine();
//string Msg=""
int intfs=Int32.Parse(strfs);
//intfs=(intfs > 60) ? Msg="及格":Msg="不及格";
intfs=(intfs > 60) ? intfs+=1:intfs-=1;
Console.WriteLine(intfs);
}
}
我用记事本书写完后在命令行编译器下编译该语句,正常通过;
但是我如果用//中的Msg来就会出现error cs0029:无法将类型"string",隐式转换为"int";
为何出此错误,请明示!谢谢!
using System;
using System.Windows.Forms;class yuan
{
public static void Main()
{
string strfs=Console.ReadLine();
//string Msg=""
int intfs=Int32.Parse(strfs);
//intfs=(intfs > 60) ? Msg="及格":Msg="不及格";
intfs=(intfs > 60) ? intfs+=1:intfs-=1;
Console.WriteLine(intfs);
}
}
我用记事本书写完后在命令行编译器下编译该语句,正常通过;
但是我如果用//中的Msg来就会出现error cs0029:无法将类型"string",隐式转换为"int";
为何出此错误,请明示!谢谢!
intfs = Msg = "及格";
else
intfs = Msg = "不及格";
赋值表达式也是表达式,是表达式就有值,如果非要用?:可以改成这样:intfs = (intfs > 60) ? (Msg="及格").Length * 0 + intfs : (Msg="不及格").Length * 0 + intfs;
改成
if(intfs > 60)
Msg="及格";
else
Msg="不及格";那种写法相当于
intfs=Msg="及格"
intfs=Msg="不及格"// 你说说能对吗?
(intfs > 60) ? Msg="及格":Msg="不及格";
e:\Visual Studio\ConsoleApplication1\ConsoleApplication1\Class1.cs(2): 类型或命名空间名称“Windows”在类或命名空间“System”中不存在(是否缺少程序集引用?)
------------
怎么运行的?你只有把using System.Windows.Forms;去掉才有可能运行-------------------------------------
intfs=(intfs > 60) ? Msg="及格":Msg="不及格";
------------------
intfs是int型,Msg是字符串型,当然有错了!
Msg = (intfs > 60) ? "及格":"不及格";
被楼主绕糊涂了……