为什么我使用?:条件运算符不正确? [英] Why is my use of the ?: conditional operator incorrect?

查看:208
本文介绍了为什么我使用?:条件运算符不正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试编译时收到编译错误,不是声明,代码是:

 (checkDatabaseExist ())?connectToDB():buildDB();当函数是:$ / b> 

p> private boolean checkDatabaseExist(){...}
private void connectToDB(){...}
private void buildDB(){...}

任何想法?

解决方案

是的,您无法使用条件运算符那样的。它的目的是计算一个表达式或另一个表达式。它不是一种选择一个语句来执行的方式。



只需使用:

  if(checkDatabaseExist()){
connectToDB();
} else {
buildDB();
}


I get a compilation error while trying to compile, "not a statement", and the code is:

(checkDatabaseExist())?connectToDB() : buildDB();

when the functions are:

private boolean checkDatabaseExist() {...}
private void connectToDB(){...}
private void buildDB(){...}

any ideas?

解决方案

Yes, you can't use the conditional operator like that. It's intended to compute one expression or another as a result. It's not intended to be a way of choosing one statement to execute or another.

Just use:

if (checkDatabaseExist()) {
    connectToDB();
} else {
    buildDB();
}

这篇关于为什么我使用?:条件运算符不正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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