C#条件运算符错误仅赋值,调用,递增,递减,等待和新对象表达式可以用作语句 [英] C# conditional operator error Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

查看:113
本文介绍了C#条件运算符错误仅赋值,调用,递增,递减,等待和新对象表达式可以用作语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个基本程序来查找输入的数字是否为质数。我有一个 checkPrime(num)函数,用于检查素数并返回 true 如果num是素数,否则返回 false 。现在,在我的 main()函数中,我使用了条件运算符来缩短代码,但是却收到了一个我不确定的错误。下面是我的代码:

Hi I was writing a basic program to find if the input number is prime or not. I have a checkPrime(num) function that checks for prime number and returns true if num is prime else returns false. Now in my main() function I used conditional operator to shorten the code but I am getting an error which I am not sure about. Below is my code :

static void Main(String[] args) {
    int n = Int32.Parse(Console.ReadLine());
    while (n-- > 0) {
        int num = Int32.Parse(Console.ReadLine());
        (checkPrime(num) == true) ? Console.WriteLine("Prime") : Console.WriteLine("Not Prime");
    }
}

编译时,得到的错误为在条件语句行的while循环中,只能将赋值,调用,递增,递减,等待和新对象表达式用作语句。我不确定我想念的是什么。在此处还有类似的问题,人们已经回答说,条件运算符行是一个表达式,而不是一个语句,因此必须对表达式的值进行某种排序或赋值。 MSDN参考中给出了相同的示例,其中的解释类似于

When I compile, I get the error as Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement in my while loop at conditional statement line. I am not sure what is it that I am missing. There is similar question here and people have answered that the conditional operator line is an expression and not a statement so there has to be some sort or assignment for the value of the expression. Same kind of example is given in MSDN reference where the explanation does something like this

// ?: conditional operator.
classify = (input > 0) ? "positive" : "negative";

但是我无法理解的是我的功能只是想检查一下函数的返回值,然后打印输出。因此,在我看来,这种表达方式在哪里?

But what I am not able to understand is in my function all I am trying to do is check the return value of the function and then print the output. So where does this expression thing comes in my case.

推荐答案

条件运算符表达式,而不是语句,这意味着它不能单独存在,因为结果必须是用某种方式。在您的代码中,您不使用结果,而是尝试产生副作用

The conditional operator is an expression, not a statement, that means it cannot stand alone, as the result has to be used somehow. In your code, you don't use the result, but try to produce side effects instead.

取决于运算符返回第一个或第二个表达式的结果。但是 Console.WriteLine()的返回类型为 void 。因此,运算符没有任何返回值。对于?:运算符, void 不是有效的返回类型。因此,此处不能使用 void 方法。

Depending on the condition before the ? the operator returns the result of either the first or the second expression. But Console.WriteLine()'s return type is void. So the operator has nothing to return. void is not a valid return type for the ?: operator. So a void-method cannot be used here.

因此您可以执行以下操作:

So you could do something like that:

while (n-- > 0) {
    int num = Int32.Parse(Console.ReadLine());
    string result = checkPrime(num) ? "Prime" : "Not Prime";
    Console.WriteLine(result);
}

或在 Console.WriteLine中使用运算符()调用:

while (n-- > 0) {
    int num = Int32.Parse(Console.ReadLine());
    Console.WriteLine(checkPrime(num) ? "Prime" : "Not Prime");
}

在两个示例中,运算符返回两个字符串之一,具体取决于在这种情况下。这就是该运算符的作用。

In both examples, the operator returns one of the two strings depending on the condition. That's what this operator is for.

请注意,您不需要比较结果 checkPrime() true 的值。结果已经是 bool

Note that you do not need to compare the result of checkPrime() to true. The result already is a bool.

这篇关于C#条件运算符错误仅赋值,调用,递增,递减,等待和新对象表达式可以用作语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆