不兼容的类型字符串和字符 [英] Incompatible Types String and Char

查看:80
本文介绍了不兼容的类型字符串和字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定为什么会出现此错误.我认为代码总体上还可以,尽管我敢肯定,然后使用所有其他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;
 }

这篇关于不兼容的类型字符串和字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆