简体   繁体   English

Java中的布尔表达式

[英]Boolean expression in Java

I'm starting to learn about boolean expressions. 我开始学习布尔表达式。 I am trying to figure out the following question: 我想弄清楚以下问题:

Suppose age1, age2, and age3 are int variables, and suppose answer is a boolean variable. 假设age1,age2和age3是int变量,并假设answer是一个布尔变量。 Write an expression that assigns answer the value true exactly when age1 is less than or equal to age2 AND age2 is less than or equal to age3. 写一个表达式,当age1小于或等于age2并且age2小于或等于age3时,该表达式将确切地分配值true。 Otherwise answer should be assigned false. 否则应将答案分配为false。

I have tried a few things but am relatively new to Java. 我尝试了一些东西,但对Java来说相对较新。 I am able to make the answer print True but something is still wrong with my numbers. 我能够将答案打印为True但我的数字仍有问题。

This is wrong: 这是错的:

age1=7;
age2=10;
age3=12;
boolean a= (age1<=age2);
boolean b= (age2<=age3);
boolean answer= (a&&b);

I'm just not sure how to fix this or what exactly is going on in the code; 我只是不确定如何解决这个或代码中究竟发生了什么; what am I doing wrong? 我究竟做错了什么?

The code your given should work perfectly. 你给出的代码应该完美地工作。

age1=7;
age2=10;
age3=12;
boolean a= (age1<=age2);
boolean b= (age2<=age3);
boolean answer= (a&&b);

But as the questions specifies an "expression" try this out: 但是,当问题指定“表达”时,请尝试以下方法:

boolean answer=age1<=age2 && age2<=age3;

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

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