您好,我正試圖學習如何編寫一個濃縮if語句沒有else{} else if{}
條件,其中代碼打印YES或NO,如果條件符合播放聲調,我試圖連接此聲明。如果陳述錯誤濃縮
Message = (UserValue == "1 2 3 4") ? "Correct" + Console.Beep(250, 250) : "Incorrect"+ Console.Beep(130, 250);
感謝,
保羅。
您好,我正試圖學習如何編寫一個濃縮if語句沒有else{} else if{}
條件,其中代碼打印YES或NO,如果條件符合播放聲調,我試圖連接此聲明。如果陳述錯誤濃縮
Message = (UserValue == "1 2 3 4") ? "Correct" + Console.Beep(250, 250) : "Incorrect"+ Console.Beep(130, 250);
感謝,
保羅。
這是唯一可能如下。在三元操作多條語句是不可能在C#中,你不能+
兩個不同的(無效和字符串)
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";
};
}
我是這麼認爲的,我得到了它的工作這樣做。使用系統; 如果(UserValue == 「1 2 3 4」) { Console.Beep(250250); } 別的 { Console.Beep(130,250); } Console.WriteLine(消息); 到Console.ReadLine(); – mrpjspencer
Console.Beep回報void
,所以你不能把它串聯到string
,這是您要在這裏做的:
"Correct" + Console.Beep(250, 250)
這裏:
"Incorrect"+ Console.Beep(130, 250)
我建議你使用正規的if語句來代替,如果你想打電話Console.Beep
請不要寫入如果這樣的「濃縮」時尚語句。它不會添加任何內容,除了在稍後階段維護代碼的默認和麻煩之外。你沒有獲得**任何**。 – Maarten