我使用這個代碼顯示的結果爲偶數或奇數,而不是真假這裏預期:三元操作符不工作
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「」 –