压缩的 if 语句错误
本文关键字:错误 语句 if 压缩 | 更新日期: 2023-09-27 18:34:41
您好,我正在尝试学习如何编写一个没有else{} else if{}
条件的压缩if语句,其中代码打印YES或NO,并在满足条件时播放音调,我正在尝试连接此语句。
Message = (UserValue == "1 2 3 4") ? "Correct" + Console.Beep(250, 250) : "Incorrect"+ Console.Beep(130, 250);
谢谢
保罗。
Console.Beep 返回 void
,因此您无法将其连接到 string
,这就是您在这里尝试执行的操作:
"Correct" + Console.Beep(250, 250)
在这里:
"Incorrect"+ Console.Beep(130, 250)
我建议你改用常规的if语句,如果你想调用Console.Beep
这只能按以下方式进行。在 C# 中不可能使用三元运算符中的多个语句,并且不能+
两个不同的语句(void 和 string(
public class Program
{
public static void Main()
{
var UserValue = "1 2 3 4";
var Message = "";
Message = (UserValue == "1 2 3 4") ? Program.x() : Program.y();
Console.WriteLine(Message);
}
static Func<string> x = () => {
Console.Beep(250, 250);
return "Correct";
};
static Func<string> y = () => {
Console.Beep(130, 250);
return "Incorrect";
};
}