Java - OR / AND字符(||&&)在if语句中是非法的 [英] Java - OR / AND characters (|| &&) illegal in if-statement

查看:5825
本文介绍了Java - OR / AND字符(||&&)在if语句中是非法的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用扫描仪编写一个简单的程序,现在想通过比较这些单词的输入来确保用户只键入是或否。我正在使用正常的,如果 -statement与 || (管道):

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(我正在使用BlueJ)抱怨我的管道,它说:非法人物:'\ u00a0'

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

我尝试了什么


  • 我复制了&来自维基百科网站的粘贴管道,以确保我真正使用正确的管道字符

  • 重新启动BlueJ

  • 尝试使用等号(&& ),同样的错误

  • 如果我删除了第二个条件及其工作的管道

  • 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

推荐答案

字符 u00a0 是一个不间断的空间。

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"))

或者只是简单地压缩你的行中的空格并重新输入它们(确保你没有输入 nbsp )。

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

请注意,我将 || 更改为&& 作为输入没有例如仍然会进入条件,因为是不等于输入。那么你的条件是 true || false 这将导致 true

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.

这篇关于Java - OR / AND字符(||&&)在if语句中是非法的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆