不兼容的类型字符串和字符
问题描述:
我不确定为什么会出现此错误.我认为代码总体上还可以,尽管我敢肯定,然后使用所有其他ifs的方法会更短.问题是,它说不兼容的类型,我真的我只是失去了对如何解决这一问题.任何帮助,将不胜感激.
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 incompatible types and I really am just lost on how to fix this. Any help would be appreciated.
import java.util.Scanner;
public class MissionImpossible
{
public static void main(String [] args){
String lineOne, R2D2 = "";
Scanner in = new Scanner(System.in);
System.out.println("Please enter a word so I can see how many vowels it has.");
int count = 0;
lineOne = in.nextLine();
int word = lineOne.length();
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++;
}
System.out.println(count);
}
}
答
char
不是 String
.将 R2D2
声明为 char
char R2D2 = '';
要检查元音,请使用以下方法,并在 for
循环和 count
元音出现的位置重复使用此方法:
To check vowel make a method like below and reuse this method at for
loop and count
the vowel occurrence:
static boolean isVowel(char ch) {
ch = Character.toLowerCase(ch);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
return true;
}
return false;
}