比较原始类型
我被问到
根据下面的 a、b 和 c 的定义,选择编译成功且计算结果为 true 的表达式。
int a = 1; 字符 b = 'a'; 布尔值 c = false;
所以我用了一个简单的
if (expression)
{System.out.println("True");}
else
{System.out.println("False");}
这样是吗?
c==a //false
!c || a //false
b >= a //true
c = a //false
a - b - 96 //false
a + b > 0 //true
c = true //true
a < b //true
这看起来还好吗?
I have been asked
Given the definitions of a, b and c below, select the expressions that compile successfully and evaluate to true.
int a = 1; char b = 'a'; boolean c = false;
So I used a simple
if (expression)
{System.out.println("True");}
else
{System.out.println("False");}
Is this right?
c==a //false
!c || a //false
b >= a //true
c = a //false
a - b - 96 //false
a + b > 0 //true
c = true //true
a < b //true
Does this look ok?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
c==a
:无法编译,int
无法与boolean
进行比较。boolean ||不允许使用 int
b >= a
:编译,计算结果为true
c = a
:不编译,无法将int
值分配给boolean
a - b - 96
:编译,计算结果为 -192a + b > ; 0
:编译,计算结果为true
c = true
:编译,计算结果为true
(并赋值true
代码> 到c
)a
b
:编译,计算结果为true
c==a
: doesn't compile,int
can't be compared toboolean
.!c || a
: doesn't compile,boolean || int
isn't allowedb >= a
: compiles, evaluates totrue
c = a
: doesn't compile, can't assign anint
value to aboolean
a - b - 96
: compiles, evaluates to -192a + b > 0
: compiles, evaluates totrue
c = true
: compiles, evaluates totrue
(and assignstrue
toc
)a < b
: compiles, evaluates totrue
如果表达式无法编译,您认为输出
会是什么?
对于那些能够编译的人来说,是的,你是对的。
If an expression doesn't compile, what do you think the output of
would be?
For those that do compile, then yes, you're right.