首页 > 解决方案 > 如何检查方法是否返回真或假?

问题描述

所以我有这个代码

public static boolean isVowel (char c) { 

        if (c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
        {
            return true;
        }

        else
        {
            return false;
        }
    }

这段代码有两个单独的方法

    if (isVowel == true()) //I know this is wrong but how could I make it work?
        {
            //run command
        }
        else 
        {
            //run command
        }

如果是真的,我怎么做if (isVowel())测试if isVowel

标签: java

解决方案


public static boolean isVowel (char c) { 
    // concise code here
    return (c=='a'|| c=='e'|| c=='i'|| c=='o'|| c=='u');
}

// your fix here
if (isVowel(aCharVariable)) {
    // your code here
} else {
    // your code here
}

简洁明了。


推荐阅读