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

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

问题描述

我正在编写一个基本程序来查找输入数字是否为质数.我有一个 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循环中的条件语句行.我不确定我错过了什么.有类似的问题 here 和人们已经回答说,条件运算符行是一个表达式而不是一个语句,因此必须对表达式的值进行某种排序或赋值.MSDN reference 中给出了相同类型的示例,其中的解释类似于这个

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.

所以你可以这样做:

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

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

or you use the operator inside the Console.WriteLine() call:

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#条件运算符错误只有赋值、调用、递增、递减、await和new对象表达式可以作为语句使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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