为“if”添加多个条件

时间:2013-10-19 01:22:39

标签: java if-statement

我正在尝试为图像的可见性添加多个条件。

我读过人们用

实现这一目标

if(something)|| (别的)|| (更多)   //做点什么

但是,当我把||在我的语句之间我得到“令牌上的语法错误,仅仅是无效的同步”

这个问题有另一个解决方案吗?

有问题的代码:

    SharedPreferences pref = getSharedPreferences("ActivityPREF", 
    Context.MODE_PRIVATE);
    int clicks = pref.getInt("Total_Clicks", 0);

//add another SharedPreferences here

    if (clicks < 25) || //add another statement here {
        mImage.setVisibility(View.GONE);
        subbtn.setVisibility(View.GONE);
    if (clicks > 25) {
        mImage.setVisibility(View.VISIBLE);
        subbtn.setVisibility(View.VISIBLE);

3 个答案:

答案 0 :(得分:2)

所有条件都必须在if语句的括号内。

if ( clicks < 25 || add another statement here ) {
    ...
}

如果需要,您可以嵌套括号。

if ( (clicks < 25) || (add another statement here) ) {
    ...
}

答案 1 :(得分:2)

'if'语句要求条件在括号内。所以试试这样:

if (condition1 || condition2 || condition3) {
}

答案 2 :(得分:1)

比尔蜥蜴是对的,但我会添加更多信息。说你有:

if((x > y) || (x + y) && (x >= 1)) {

// do stuff

}

正如比尔所说,你需要在括号中加入你的条件,因为你想先让它们进行评估。其次,您还应该了解偶数逻辑运算符的优先顺序。订单如下:

  1. !(not)
  2. &&(and)
  3. ||(or)
  4. 希望这有帮助