Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
编写复杂的Java布尔表达式_Java - Fatal编程技术网

编写复杂的Java布尔表达式

编写复杂的Java布尔表达式,java,Java,我试图写一个代码,如果满足以下条件,它会说嫁给我,否则它会说迷路 需要满足的条件: 22至27岁男性,不吸烟,身高不超过72英寸,体重不超过160磅,相貌英俊,能够搬迁。” 这是我到目前为止写的代码。 但是,即使我认为它符合给定的标准,它也会丢失?怎么了 此外,我如何用一个布尔值来表示呢 public class marry { public static void main(String[] args) { int weight = 150; int

我试图写一个代码,如果满足以下条件,它会说嫁给我,否则它会说迷路

需要满足的条件:

22至27岁男性,不吸烟,身高不超过72英寸,体重不超过160磅,相貌英俊,能够搬迁。”

这是我到目前为止写的代码。 但是,即使我认为它符合给定的标准,它也会丢失?怎么了

此外,我如何用一个布尔值来表示呢

public class marry {

    public static void main(String[] args) {
        int weight = 150;
        int age = 24;
        int height = 71;
        boolean isASmoker = false;
        boolean isMale = true;
        boolean isGoodLooking = true;
        boolean isAbleToRelocate = true;

        if (((weight < 160 && (age <= 27 && age >= 22)) && ((height < 72) && ((isASmoker = false) && (isMale = true))) && ((isGoodLooking = true) && (isAbleToRelocate = true)))) {
            System.out.println("Marry Me!");
        }
        else {
            System.out.println("Get Lost!");
        }
    }
}
公共类{
公共静态void main(字符串[]args){
整数重量=150;
年龄=24岁;
整数高度=71;
布尔值isASmoker=false;
布尔值isMale=true;
布尔值isGoodLooking=true;
布尔值IsabletoreLocation=true;
如果((体重<160&&(年龄=22))&&((身高<72)&&((Isasmocker=false)&&((isMale=true))&((isGoodLooking=true)&&((IsabletoreLocation=true))){
System.out.println(“嫁给我!”);
}
否则{
System.out.println(“滚开!”);
}
}
}

谢谢

您的布尔表达式不正确。单个“=”执行赋值。您需要“==”进行布尔比较

改变

isASmoker = false


至于可读性,可以通过删除不必要的括号并将(variable==false)更改为(variable==false)来清理整个表达式!变量这是一个偏好问题,尽管我想大多数人都会同意,正如我所写的,这个表达式很长,很难理解。

试试我的一段代码来决定你的问题:

class Person {
    private int weight;
    private int age;
    private int height;
    private boolean isASmoker;
    private boolean isMale;
    private boolean isGoodLooking;
    private boolean isAbleToRelocate;

    public Person(int weight, int age, int height, boolean isASmoker, boolean isMale, boolean isGoodLooking, boolean isAbleToRelocate) {
        this.weight = weight;
        this.age = age;
        this.height = height;
        this.isASmoker = isASmoker;
        this.isMale = isMale;
        this.isGoodLooking = isGoodLooking;
        this.isAbleToRelocate = isAbleToRelocate;
    }

    public boolean isGood() {
        return weight < 160 && age <= 27 && age >= 22 && 
               height < 72 && !isASmoker && isMale && 
               isGoodLooking && isAbleToRelocate;
               // (variable == true) => variable
               // (variable == false) => !variable
    }

    public static void main(String[] args) {
        Person person = new Person(150, 24, 71, false, true, true, true);
        System.out.println(person.isGood() ? "Marry Me!" : "Get Lost!");
    }
}
班级人员{
私有整数权重;
私人互联网;
私人内部高度;
私人烟民;
私有布尔isMale;
私家车很好看;
私有布尔isAbleToRelocate;
公众人物(整数体重、整数年龄、整数身高、布尔Isasmocker、布尔isMale、布尔isGoodLooking、布尔IsabletoreLocation){
重量=重量;
这个。年龄=年龄;
高度=高度;
this.isasmocker=isasmocker;
this.isMale=isMale;
this.isGoodLooking=isGoodLooking;
this.isAbleToRelocate=isAbleToRelocate;
}
公共布尔值是好的(){
返回重量<160&&age=22&&
身高<72&&!吸烟者
isGoodLooking&&IsabletoreLocation;
//(变量==真)=>变量
//(变量==false)=>!变量
}
公共静态void main(字符串[]args){
Person-Person=新人(150、24、71、假、真、真、真);
System.out.println(person.isGood()?“嫁给我!”:“滚开!”);
}
}

正如EJK所说的
=
是一个赋值操作符,但是在检查
布尔值时,您甚至不需要
=
,因此更新以匹配以下内容

isasmocker=false
!吸烟者

isMale=true
isMale

公共类{
 public class marry {

    public static void main(String [] args) {

               int weight = 150;
               int age = 24;
              int height = 71;
              boolean isASmoker = false;
              boolean isMale = true;
              boolean isGoodLooking = true;
              boolean isAbleToRelocate = true;

        if ( ( ( weight < 160 && ( age <= 27 && age >= 22 ) ) && ( ( height < 72 ) && ( ( isASmoker == false ) && ( isMale) ) ) && ( ( isGoodLooking ) && ( isAbleToRelocate ) ) ) ) {
           System.out.println("Marry Me!");
        } else {
           System.out.println("Get Lost!");
        }
   }
}
公共静态void main(字符串[]args){ 整数重量=150; 年龄=24岁; 整数高度=71; 布尔值isASmoker=false; 布尔值isMale=true; 布尔值isGoodLooking=true; 布尔值IsabletoreLocation=true; 如果((体重<160&&(年龄=22))&&((身高<72)&&((Isasmocker==假)&&&(isMale))&&((isGoodLooking)&&(IsabletoreLocation))){ System.out.println(“嫁给我!”); }否则{ System.out.println(“滚开!”); } } }
试着这样做,可能会奏效


如果要检查布尔值是否为真,甚至不必使用
=
运算符。您可以在if语句中正确选择布尔值(
isMale
)。最初出现错误的地方是在if语句中使用
=
操作符,而不是
=
操作符,但我为您清理了更多

避免超过80个字符的行。(c)jcc@AndrewTobilko抱歉,我会马上修复它,您使用
=
来比较值,您必须在java中使用
=
。嗨@f1sh我如何使用一个布尔值来代替?在哪里?在命令上方的布尔值中,或者在if@EJKBetter的其他代码旁边的底部,只需删除比较并单独使用布尔变量即可。@EJK还有,我如何将所有这些代码转换为一个布尔表达式?这一点很好。是的,这方面还有很大的改进空间。@BilltheLizard我怎样才能放弃比较?
isMale == true
class Person {
    private int weight;
    private int age;
    private int height;
    private boolean isASmoker;
    private boolean isMale;
    private boolean isGoodLooking;
    private boolean isAbleToRelocate;

    public Person(int weight, int age, int height, boolean isASmoker, boolean isMale, boolean isGoodLooking, boolean isAbleToRelocate) {
        this.weight = weight;
        this.age = age;
        this.height = height;
        this.isASmoker = isASmoker;
        this.isMale = isMale;
        this.isGoodLooking = isGoodLooking;
        this.isAbleToRelocate = isAbleToRelocate;
    }

    public boolean isGood() {
        return weight < 160 && age <= 27 && age >= 22 && 
               height < 72 && !isASmoker && isMale && 
               isGoodLooking && isAbleToRelocate;
               // (variable == true) => variable
               // (variable == false) => !variable
    }

    public static void main(String[] args) {
        Person person = new Person(150, 24, 71, false, true, true, true);
        System.out.println(person.isGood() ? "Marry Me!" : "Get Lost!");
    }
}
 public class marry {

    public static void main(String [] args) {

               int weight = 150;
               int age = 24;
              int height = 71;
              boolean isASmoker = false;
              boolean isMale = true;
              boolean isGoodLooking = true;
              boolean isAbleToRelocate = true;

        if ( ( ( weight < 160 && ( age <= 27 && age >= 22 ) ) && ( ( height < 72 ) && ( ( isASmoker == false ) && ( isMale) ) ) && ( ( isGoodLooking ) && ( isAbleToRelocate ) ) ) ) {
           System.out.println("Marry Me!");
        } else {
           System.out.println("Get Lost!");
        }
   }
}