有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法使用bool char方法将char转换为字符串

public class main {

    public static void main(String[] args) {
        isVowelTester();
    }

    public static boolean isVowel(char ch) {
        /*
        This method returns true if ch is a vowel 
        and returns false if ch is any other character. 
        Vowels are the letters a, e, i, o, and u.
         */
        ch = "cod";
        String str = Character.toString(ch);

        if (str.contains("a" + "A" + "e" + "E" + "i" + "I" + "o" + "O" + "u" + "U")) {
            return true;
        } else {
            return false;
        }

    }

    public static char isVowelTester() {
        return isVowelTester(System.out.println("Supplied word has vowels: " + isVowel()));
    }
}

它在ch=“cod”处给我一个错误;它不能被转换。我查阅了许多关于如何将其从char转换为string的示例,尽管它仍然会给我一些与转换相关的错误类型

有人能给我一些建议吗


共 (4) 个答案

  1. # 1 楼答案

    String.valueOf(char)似乎是将字符转换为字符串的最有效方法

    要查看所有方法,请查看链接6 ways to convert

  2. # 2 楼答案

    public class Main {
    
        public static void main(String[] args) {
            isVowelTester();
        }
    
        public static boolean isVowel(char ch) {
            /*
             * This method returns true if ch is a vowel and returns false if ch is
             * any other character. Vowels are the letters a, e, i, o, and u.
             */
    
            String str = new String(ch + "");
            String inMatchString  = "AaEeIiOoUu";
    
            if (inMatchString.contains(str)) {
                return true;
            } else {
                return false;
            }
    
        }
    
        public static void isVowelTester() {
            System.out.println("Supplied word has vowels: " + isVowel('a'));
        }
    }
    

    希望这能有所帮助

  3. # 3 楼答案

    哦我的。。。这真是一团糟。首先看一下方法public static char isVowelTester()。你在这个方法中调用这个方法,我的意思是你在使用递归。您最终将获得StackOverflowError,因为您只返回相同的方法作为返回,并且没有其他可能停止调用isVowelTester(),因此它将无休止地调用(直到溢出)
    您只需打印结果,无需返回:

    public static void isVowelTester() {
        System.out.println("Supplied word has vowels: " + isVowel()));
    }
    

    但这还没有结束。在方法public static boolean isVowel(char ch)中,有一个参数char ch。你必须给这个方法一个参数。所以我们之前的代码必须更新以给出这个参数

    public static void isVowelTester() {
        char ch = 'a';
        System.out.println("Supplied word has vowels: " + isVowel(ch)));
    }
    

    请注意,在分配字符时,我使用的是“”,而不是“”。这是字符串和字符之间的区别
    最后,我们可以使用方法isVowel(char ch)。您不能将"cod"分配给char,因为这是3个字母,而char,正如名字所说,只是一个字符。还使用了引号,字符串中使用的是什么
    if语句也是错误的,因为您不是在比较contains(...)参数中的每个字符,而是在比较“aaeeiiouu”,因为+符号用于连接字符串。你只是简单地连接元音,然后再和一个字符进行比较,这永远不会是真的。更好的方法是颠倒你的说法:

    public static boolean isVowel(char ch) {
        String str = Character.toString(ch);
    
        if ("AaEeIiOoUu".contains(str)) {
            return true;
        } else {
            return false;
        }
    }
    

    这还没完。您可以对此进行优化,因为您基本上是在返回if的结果。注意,如果条件为真,则返回真,否则返回假。所以你可以写:return "AaEeIiOoUu".contains(str)。这就是你要做的

  4. # 4 楼答案

    public static boolean isVowel(char ch) {
        return "AaEeIiOoUu".contains(Character.toString(ch));
    }
    
    ch = 'y'; // possible
    ch = "n"; // not possible a string cannot be assigned to a char variable