Incompatible Types String and Char

前端 未结 4 1954
挽巷
挽巷 2021-01-17 01:13

I\'m not sure why I\'m getting this error. I think the code in general is okay although I\'m sure there is a shorter way then using all the else ifs. The problem is it says

4条回答
  •  孤街浪徒
    2021-01-17 01:55

        String lineOne,  R2D2 = "";
    

    R2D2 is a string and you are comparing with a char if (R2D2== 'a'|| R2D2=='A')

    try this

    for (int i = word -1; i>= 0; i--) { R2D2= lineOne.charAt(i);

            if (R2D2=="a"|| R2D2=="A")
            count++;
            else if (R2D2=="e"||R2D2=="E")
            count++;
            else if (R2D2=="o"|| R2D2=="O")
            count++;
            else if (R2D2=="u"||R2D2=="U")
            count++;
            else if (R2D2=="y"||R2D2=="Y")
            count++;
    
    }
    

    One side not,you should compare using .equals() instead of ==

提交回复
热议问题