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

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

If condition Error on compiling - Specific question

问题

我正在尝试检测变量 f1f2 是否含有字母 "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())) {

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:

确定