为什么我不能在C#三元表达式中使用break? [英] Why can't I use break in a C# ternary expression?
本文介绍了为什么我不能在C#三元表达式中使用break?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在while循环中将if else子句转换为三元数,但是它不允许我在问号后休息,将错误指向中断作为无效表达式。
I am trying to convert the if else clause to a ternary within a while loop, however it's not allowing me to have a break after the question mark, pointing an error at the break as an invalid expression.
如何将这种简单的方法转换成三元的方法。
How would I go about turning this simple if else into a ternary like so.
while (true)
{
Console.WriteLine("Enter 3 words seperated by spaces: ");
var input = Console.ReadLine();
//input == "" ? break : ConvertToPascal(input);
if (input == "")
break;
else
ConvertToPascal(input);
}
}
推荐答案
它使用三元运算符是不可能的,但是您可以按如下所示简化代码结构:
It isn't possible using the ternary operator, but you can simplify your code structure as follows:
string input;
do {
Console.WriteLine("Enter 3 words seperated by spaces: ");
input = Console.ReadLine();
if (input != "") {
ConvertToPascal(input);
}
} while(input != "");
这篇关于为什么我不能在C#三元表达式中使用break?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文