我使用这个代码显示的结果为偶数或奇数,而不是真假这里预期:三元操作符不工作
Console.WriteLine(" is " + result == true ? "even" : "odd");
因此我使用三元运算符,但它抛出的错误,一些语法问题在这里,但我无法捕捉它。 由于提前
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplicationForTesting
{
delegate int Increment(int val);
delegate bool IsEven(int v);
class lambdaExpressions
{
static void Main(string[] args)
{
Increment Incr = count => count + 1;
IsEven isEven = n => n % 2 == 0;
Console.WriteLine("Use incr lambda expression:");
int x = -10;
while (x <= 0)
{
Console.Write(x + " ");
bool result = isEven(x);
Console.WriteLine(" is " + result == true ? "even" : "odd");
x = Incr(x);
}
因为在函数中没有返回。你期望从IsEven返回布尔(只是猜测)。 Easiset解决方案,只需在Visual Studio中双击该行,它就会带您进入发生错误的**精确行**。 – Tigran
而错误信息并没有帮助你? – leppie
@leppie,错误是“”运算符'=='不能应用于'string'和'bool'类型的操作数\t“” –