如果编译时出现条件错误 – 具体问题

huangapple go评论91阅读模式
英文:

If condition Error on compiling - Specific question

问题

我正在尝试检测变量 f1f2 是否含有字母 "v" 或 "b",然后检查这两个单词是否具有相同的长度。我不知道为什么,但当我运行这段代码时,它显示我的 if 条件中有三个错误。

  1. Scanner ask = new Scanner(System.in);
  2. String f1 = (ask.nextLine()).toLowerCase();
  3. String f2 = (ask.nextLine()).toLowerCase();
  4. boolean yes = false;
  5. if ((f1.indexOf("v") > -1 || f1.indexOf("b")) && (f2.indexOf("v") > -1 || f2.indexOf("b")) && (f1.length() == f2.length())) {
  6. yes = true;
  7. }

编译错误:

  1. Solution.java:12: error: '}' expected
  2. if ((wrd.indexOf("v") > -1 || wrd.indexOf("b")) and (wrd2.indexOf("v") > -1 || wrd2.indexOf("b")) and (wrd.length() == wrd2.length())) {
  3. ^
  4. Solution.java:12: error: ';' expected
  5. if ((wrd.indexOf("v") > -1 || wrd.indexOf("b")) and (wrd2.indexOf("v") > -1 || wrd2.indexOf("b")) and (wrd.length() == wrd2.length())) {
  6. ^
  7. Solution.java:12: error: ';' expected
  8. if ((wrd.indexOf("v") > -1 || wrd.indexOf("b")) and (wrd2.indexOf("v") > -1 || wrd2.indexOf("b")) and (wrd.length() == wrd2.length())) {
  9. ^
  10. 3 errors
  11. Exit Status
  12. 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

  1. Scanner ask = new Scanner(System.in);
  2. String f1 = (ask.nextLine()).toLowerCase();
  3. String f2 = (ask.nextLine()).toLowerCase();
  4. boolean yes = false;
  5. if((f1.indexOf("v") > -1 || f1.indexOf("b")) and (f2.indexOf("v") > -1 || f2.indexOf("b")) and (f1.length() == f2.length() )){
  6. yes = true;
  7. }

Error at compile

  1. Solution.java:12: error: ')' expected
  2. if((wrd.indexOf("v") > -1 || wrd.indexOf("b")) and (wrd2.indexOf("v") > -1 || wrd2.indexOf("b")) and (wrd.length() == wrd2.length() )){
  3. ^
  4. Solution.java:12: error: ';' expected
  5. if((wrd.indexOf("v") > -1 || wrd.indexOf("b")) and (wrd2.indexOf("v") > -1 || wrd2.indexOf("b")) and (wrd.length() == wrd2.length() )){
  6. ^
  7. Solution.java:12: error: ';' expected
  8. if((wrd.indexOf("v") > -1 || wrd.indexOf("b")) and (wrd2.indexOf("v") > -1 || wrd2.indexOf("b")) and (wrd.length() == wrd2.length() )){
  9. ^
  10. 3 errors
  11. Exit Status
  12. 1

答案1

得分: 2

在Java中,逻辑"and"运算符表示为&&,而不是单词and

  1. if ((f1.indexOf("v") > -1 || f1.indexOf("b")) &&
  2. (f2.indexOf("v") > -1 || f2.indexOf("b")) &&
  3. (f1.length() == f2.length())) {
英文:

The logical "and" operator in Java is &&, not the word and:

  1. if ((f1.indexOf("v") > -1 || f1.indexOf("b")) &&
  2. (f2.indexOf("v") > -1 || f2.indexOf("b")) &&
  3. (f1.length() == f2.length())) {

huangapple
  • 本文由 发表于 2020年9月13日 10:10:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63866526.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定