java or,Java - OR / AND字符(||&&)在if语句中是非法的

I am coding a simple program using a scanner and now want to make sure the user only types "yes" or "no" by comparing the input to these words. I'm using a normal if-statement with || (pipes):

if (!input.equals("yes") || !input.equals("no")) {

//do something

}

Java (I am using BlueJ) complains about my pipes, it says: "illegal character: '\u00a0'"

What I tried

I copied & pasted pipes from the wikipedia site to make sure I am really using correct pipe-characters

restarted BlueJ

tried using equal signs (&&), same error

if I removed the second condition and the pipes it works

Does anyone have an idea why it complains and how I can fix it?

– Thanks in advance

解决方案

The character u00a0 is a non-breaking space.

It seems like you typed a control char in your code, replace everything from a copy of this line

if(!input.equals("yes") && !input.equals("no"))

Or simply suppress the spaces in your line and re-type them (making sure you don't type in nbsp).

Notice that I changed the || to && as typing "no" for example would still enter the condition as "yes" would not equals to input. Your condition would then be true || false which would result in true.

In any case, this is not related to the || or && operator, it is the spaces.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值