标签: conditional-operator

每个if-else构造都可以用等效的条件表达式替换吗?

(我不太需要这个答案,我只是好奇.)

是否可以使用条件运算符将每个if-else构造替换为等效的条件表达式?:

c c++ conditional-operator

8
推荐指数
1
解决办法
4591
查看次数

Java?:vb.net中的运算符

?:在.net中是否有等效的运算符?例如在java中我可以这样做:

retParts[0] = (emailParts.length > 0) ? emailParts[0] : "";
Run Code Online (Sandbox Code Playgroud)

而不是

if (emailParts.length > 0) {
    retParts[0] = emailParts[0];
} else {
    retParts[0] = "";
}
Run Code Online (Sandbox Code Playgroud)

我希望能够在VB.NET中做类似的事情.

java vb.net language-features conditional-operator

8
推荐指数
1
解决办法
1651
查看次数

如果+可空类型(C#)的简写

以下返回

无法确定条件表达式的类型,因为"double"和"<null>"之间没有隐式转换

aNullableDouble = (double.TryParse(aString, out aDouble) ? aDouble : null)
Run Code Online (Sandbox Code Playgroud)

之所以我不能只使用aNullableBool而不是使用aDouble的往返是因为aNullableDouble是生成的EntityFramework类的一个属性,它不能用作超标.

c# conditional-operator

7
推荐指数
2
解决办法
3504
查看次数

使用C#条件运算符的编译器错误

我似乎无法在谷歌上找到我需要的东西,打赌我会在这里得到快速答案.

    String str;
    bool b = true;
    b ? str="true" : str="false";

    Console.Out.WriteLine(str);
Run Code Online (Sandbox Code Playgroud)

那个?:语法对我来说是正确的.我收到了编译器错误.

Program.cs(
13,28 ):错误CS1002 :; 期望的
Program.cs(
13,28 ):错误CS1525:无效的表达式术语':'
Program.cs(13,30):
错误CS1002 :; 预期

不确定csharp语法,但是在cpp中构建.请帮忙!谢谢!

更新:大约10个人给出正确的答案LOL,所以我只会奖励提交它的第一个人.

有趣的语法,我认为我实际上比c ++语法更喜欢它.

我这样做的实际代码是:

ftp.ConnectMode = job.FTPUsePassiveMode ? FTPConnectMode.PASV : FTPConnectMode.ACTIVE;
Run Code Online (Sandbox Code Playgroud)

c# conditional-operator

7
推荐指数
2
解决办法
1149
查看次数

使用条件运算符比较两个无符号值时的有符号/无符号不匹配

我有以下C代码:

unsigned int a;
unsigned char b, c;
void test(void) {
    if (a < b)
        return;
    if (a < (b ? b : c))
        return;
}
Run Code Online (Sandbox Code Playgroud)

当我编译它(使用Microsoft cl,来自MS SDK 7,-W3警告级别)时,第二个比较会发出警告:C4018,签名/无符号不匹配.第一次比较没有发出警告.

我已经在条件运算符上检查了MS文档,并且他们说如果两个操作数是相同的类型,结果将是相同的类型,所以它应该作为第一个比较.我错过了什么吗?

UPD:测试gcc -Wall -Wextra -pedantic并且没有任何警告.

c conditional-operator visual-c++

7
推荐指数
1
解决办法
5380
查看次数

"或"有条件的Python麻烦

我正在学习Python,而且我遇到了一些问题.在我正在考虑的课程中看到类似的东西后,想出了这个简短的剧本.我之前用过"或"用"if"成功(这里没有显示太多).出于某种原因,我似乎无法让这个工作:

test = raw_input("It's the flying circus! Cool animals but which is the best?")
x = test.lower()

if x == "monkey" or "monkeys":
    print "You're right, they are awesome!!"
elif x != "monkey" or "monkeys":
    print "I'm sorry, you're incorrect.", x[0].upper() + x[1:], "is not the right animal."
Run Code Online (Sandbox Code Playgroud)

但是效果很好:

test = raw_input("It's the flying circus! Cool animals but which is the best?")
x = test.lower()

if x == "monkey":
    print "You're right, they are awesome!!"
elif x != "monkey":
    print …
Run Code Online (Sandbox Code Playgroud)

python conditional-operator conditional-statements

7
推荐指数
2
解决办法
5万
查看次数

为什么没有Swift nil-coalescing三元运算符返回unwrapped类型?

我读到三元运算符??如果不是nil则解包一个可选项,但如果我这样做:

var type: String?
type = "milk"
let certainType = type ?? "melon"
Run Code Online (Sandbox Code Playgroud)

那么CertainType仍然是a String?,如果我这样做的话

println("it's a \(certainType)")
Run Code Online (Sandbox Code Playgroud)

它将打印:

it's a Optional("milk")
Run Code Online (Sandbox Code Playgroud)

思考?

更新:

抱歉混淆 - 我的意思是var类型:字符串?

我知道它应该打印"它是牛奶",但我在控制台中看到的是"它是一个可选的("牛奶")" - 其他人遇到了同样的问题?这可能是由字符串插值引起的吗?

@Antonio提问,这里有更多的上下文和实际代码和日志记录快照 - 类型来自Note,它是一个用于处理xcdatamodel的NSManagedObject类

class Note: NSManagedObject {
  @NSManaged var type: String?
}
Run Code Online (Sandbox Code Playgroud)

我在某个时候将类型设置为'todo',然后使用以下代码将它们打印出来:

println("type class:\(_stdlib_getDemangledTypeName(note.type))")
let type1 = note.type ?? "note"
println("type1:\(type1)")
let type2: String = note.type ?? "note"
println("type2:\(type2)")
Run Code Online (Sandbox Code Playgroud)

并输出:

type class:Swift.Optional
type1:Optional("todo")
type2:todo
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,如果我没有明确地将type1的类型标记为String,它将打印不需要的结果Optional("todo") - 我在字符串插值中使用类型来构造路径,因此它很重要

conditional-operator swift

7
推荐指数
1
解决办法
4392
查看次数


C#条件运算符错误只有赋值,调用,递增,递减,等待和新对象表达式才能用作语句

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

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

当我编译时,我Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement在条件语句行的while循环中得到错误.我不确定我错过了什么.有类似的问题在这里,人们已经回答了条件运算符线是一个表达式,而不是语句,因此必须有对表达式的值某种或转让.在MSDN参考中给出了相同类型的示例,其中解释执行类似的操作

// ?: conditional operator.
classify = (input > 0) ? "positive" : "negative";
Run Code Online (Sandbox Code Playgroud)

但是我无法理解的是在我的函数中我所要做的就是检查函数的返回值然后打印输出.那么这个表达式的东西会出现在我的案例中.

c# expression compiler-errors conditional-operator

7
推荐指数
1
解决办法
2万
查看次数

C++ 中参数类型之间的私有继承情况下的三元运算符类型选择

E1 ? E2 : E3在不同类型的情况下,C++ 条件(三元)运算符的类型,如果另一个操作数可以唯一地转换为该类型,则选择其中之一E2E3这组规则实际上更复杂,总结在https://en.cppreference.com/w/cpp/language/operator_other#Conditional_operator

在下面的简化程序中,条件运算符的操作数是A和类型的右值B

class A;
struct B {
    B() {}
    B(const A &) {}
};
class A : B {};

int main() {
    void( true ? B{} : A{} );
}
Run Code Online (Sandbox Code Playgroud)

由于A是从 私有派生的B,因此不可能强制转换为父级,但A可以唯一地转换为B使用构造函数B::B(const A &)。并且没有从B到 的演员表A。所以我希望三元运算符的类型B与转换为该类型的最后一个参数相同。事实上,Clang 接受了该计划。

但 GCC 在这里打印错误:

error: 'B' is an inaccessible base of 'A'
Run Code Online (Sandbox Code Playgroud)

演示: https: …

c++ conditional-operator language-lawyer

7
推荐指数
0
解决办法
164
查看次数