简体   繁体   English

简单的java程序返回不正确的值

[英]simple java program returning incorrect value

Hello I am brushing up on Java practice.你好,我正在复习 Java 实践。 I have this very simple program: There are two int values, along with a boolean variable.我有一个非常简单的程序:有两个 int 值,以及一个布尔变量。

i) The program will return true if one of these values is negative and one is positive. i) 如果这些值之一为负而一个为正,则程序将返回真。

ii) However, if the boolean variable is true, the program can only return true if both int values are negative. ii) 但是,如果布尔变量为真,则程序只能在两个 int 值都为负时返回真。

I have tested this out with a bunch of int values but the program seems to break once I give the method the values of (1, -1), along with the boolean value set to true.我已经用一堆 int 值对此进行了测试,但是一旦我给该方法的值 (1, -1) 以及设置为 true 的布尔值,程序似乎就会中断。 Any help or explanation would be much appreciated.任何帮助或解释将不胜感激。

   public static void main(String[] args) {
        System.out.println(posNeg(1,-1,true));
    }
    
    public static String posNeg(int a, int b, boolean negative){
        if(negative && (a<0 && b<0)){
            return "true";
        }else if(!negative && (a<0 && b>0) || (a>0 && b<0)){
            return "true";
        }
        return "false";
    }

Your code breaks because of && priority .由于&& priority ,您的代码中断。

Wrap ||包裹|| condition in brackets:括号中的条件:

public static void main(String[] args) {
    System.out.println(posNeg(1,-1,true));
}

public static String posNeg(int a, int b, boolean negative){
    if(negative && (a<0 && b<0)){
        return "true";
    }else if(!negative && ((a<0 && b>0) || (a>0 && b<0))){
        return "true";
    }
    return "false";
}

&& has higher precedence than || && 的优先级高于 || , so in else if condition ie !negative && (a<0 && b>0) || , 所以在 else if 条件即 !negative && (a<0 && b>0) || (a>0 && b<0) first this condition is evaluated !negative && (a<0 && b>0) then the result of this is evaluated with res || (a>0 && b<0) 首先评估这个条件 !negative && (a<0 && b>0) 然后用 res || 评估结果(a>0 && b<0). (a>0 && b<0)。

In your case I guess you want to first evaluate ||在你的情况下,我猜你想首先评估 || then && , so just put a bracket like this in !negative && ( (a<0 && b>0) || (a>0 && b<0)) , so that first the condition inside bracket is evaluated , then that result is evaluated with &&然后 && ,所以只需将这样的括号放在 !negative && ( (a<0 && b>0) || (a>0 && b<0)) 中,以便首先评估括号内的条件,然后计算结果用 && 评估

public class Main
{
     public static void main(String[] args) {
        System.out.println(posNeg(1,-1,true));
    }
    
    public static String posNeg(int a, int b, boolean negative){
        if(negative && (a<0 && b<0)){
            return "true";
        }else if(!negative && ((a<0 && b>0) || (a>0 && b<0))){
            return "true";
        }
        return "false";
    }
}

public static void main(String[] args) {
    System.out.println(posNeg(1,-1,false));
}

public boolean posNeg(int a, int b, boolean negative) { if (negative) return (a < 0 && b < 0); public boolean posNeg(int a, int b, boolean negative) { if (negative) return (a < 0 && b < 0); else return ((a < 0 && b > 0) || (a > 0 && b < 0));否则返回 ((a < 0 && b > 0) || (a > 0 && b < 0)); } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM