英文:
If condition Error on compiling - Specific question
问题
我正在尝试检测变量 f1
和 f2
是否含有字母 "v" 或 "b",然后检查这两个单词是否具有相同的长度。我不知道为什么,但当我运行这段代码时,它显示我的 if 条件中有三个错误。
Scanner ask = new Scanner(System.in);
String f1 = (ask.nextLine()).toLowerCase();
String f2 = (ask.nextLine()).toLowerCase();
boolean yes = false;
if ((f1.indexOf("v") > -1 || f1.indexOf("b")) && (f2.indexOf("v") > -1 || f2.indexOf("b")) && (f1.length() == f2.length())) {
yes = true;
}
编译错误:
Solution.java:12: error: '}' expected
if ((wrd.indexOf("v") > -1 || wrd.indexOf("b")) and (wrd2.indexOf("v") > -1 || wrd2.indexOf("b")) and (wrd.length() == wrd2.length())) {
^
Solution.java:12: error: ';' expected
if ((wrd.indexOf("v") > -1 || wrd.indexOf("b")) and (wrd2.indexOf("v") > -1 || wrd2.indexOf("b")) and (wrd.length() == wrd2.length())) {
^
Solution.java:12: error: ';' expected
if ((wrd.indexOf("v") > -1 || wrd.indexOf("b")) and (wrd2.indexOf("v") > -1 || wrd2.indexOf("b")) and (wrd.length() == wrd2.length())) {
^
3 errors
Exit Status
1
英文:
I am trying to detect if the variables f1
and f2
have the letter v or b and then check if both words have the same length. I do not know why but when I run this code says that there are three errors on my if condition
Scanner ask = new Scanner(System.in);
String f1 = (ask.nextLine()).toLowerCase();
String f2 = (ask.nextLine()).toLowerCase();
boolean yes = false;
if((f1.indexOf("v") > -1 || f1.indexOf("b")) and (f2.indexOf("v") > -1 || f2.indexOf("b")) and (f1.length() == f2.length() )){
yes = true;
}
Error at compile
Solution.java:12: error: ')' expected
if((wrd.indexOf("v") > -1 || wrd.indexOf("b")) and (wrd2.indexOf("v") > -1 || wrd2.indexOf("b")) and (wrd.length() == wrd2.length() )){
^
Solution.java:12: error: ';' expected
if((wrd.indexOf("v") > -1 || wrd.indexOf("b")) and (wrd2.indexOf("v") > -1 || wrd2.indexOf("b")) and (wrd.length() == wrd2.length() )){
^
Solution.java:12: error: ';' expected
if((wrd.indexOf("v") > -1 || wrd.indexOf("b")) and (wrd2.indexOf("v") > -1 || wrd2.indexOf("b")) and (wrd.length() == wrd2.length() )){
^
3 errors
Exit Status
1
答案1
得分: 2
在Java中,逻辑"and"运算符表示为&&
,而不是单词and
:
if ((f1.indexOf("v") > -1 || f1.indexOf("b")) &&
(f2.indexOf("v") > -1 || f2.indexOf("b")) &&
(f1.length() == f2.length())) {
英文:
The logical "and" operator in Java is &&
, not the word and
:
if ((f1.indexOf("v") > -1 || f1.indexOf("b")) &&
(f2.indexOf("v") > -1 || f2.indexOf("b")) &&
(f1.length() == f2.length())) {
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论