如何使用单个if语句测试字符是否为元音字符?

huangapple go评论76阅读模式
英文:

How can I test if a character is a vowel using a single if statement?

问题

if (valor == 'a' || valor == 'e' || valor == 'i' || valor == 'o' || valor == 'u')
    System.out.println("xdd");
英文:

How can I test if a single character is a vowel, using a single if statement?

More practically, how can I consolidate my current logic below into a single if statement?

if (valor == 'a'); System.out.println("xdd");
if (valor == 'e'); System.out.println("xdd");
if (valor == 'i'); System.out.println("xdd");
if (valor == 'o'); System.out.println("xdd");
if (valor == 'u'); System.out.println("xdd");

答案1

得分: 3

试试这个。无论 valor 是字符还是字符串,这都会起作用。

if ("aeiou".indexOf(valor) >= 0) {
    System.out.println("xdd");
}
英文:

Try this. This will work whether valor is a character or a string.

if ("aeiou".indexOf(valor) >= 0) {
	System.out.println("xdd");
}

答案2

得分: 3

你可以尝试使用String.indexOf()

final String vowels = "aeiou";
char valor = 'e';
if (vowels.indexOf(valor) != -1) {
    System.out.println("xdd");
}
英文:

You can try String.indexOf()

final String vowels = "aeiou";
char valor = 'e';
if (vowels.indexOf(valor) != -1) {
    System.out.println("xdd");
}

答案3

得分: 1

你可以利用 || 运算符,它在语句中的作用类似于 or,用法如下:

if (valor == 'a' || valor == 'e' || valor == 'i' || valor == 'o' || valor == 'u') {
    System.out.println("xdd");
}

因为在这种情况下,它将始终输出相同的内容。

英文:

You can leverage the || operator which basically serves as a or in a statement like so:

if(valor=='a'||valor=='e'||valor=='i'||valor=='o'||valor=='u'){
    System.out.println("xdd");
}

Since in this case, it will always print the same thing.

答案4

得分: 1

一个简单的方法是使用索引字符串,并检查您的变量是否是该字符串的一部分。

String vowels = "aeiou";
if (vowels.indexOf(valor) >= 0) {
    // 随后进行必要的操作
}
英文:

One easy method, which can also be easily expanded, is using an index string and checking if your variable is part of that String.

String vowels = "aeiou";
if (vowels.indexOf(valor) >= 0) {
    // do whatever you have to
}

答案5

得分: 1

valor 是一个转换为字符串的字符,然后:

if ("AaEeIiOoUu".contains(valor)) {
    System.out.println("xdd");
}
英文:

Let valor is a character converted to String, then:

if ("AaEeIiOoUu".contains(valor)) {
    System.out.println("xdd");
}

答案6

得分: 0

使用正则表达式。

字符串 str = "a";

if (str.matches("[AaeEiIoOuU]")) {
System.out.println("It's a vowel!");
} else {
System.out.println("it's not a vowel!");
}

英文:

Use a regex.

String str = "a";

if (str.matches("[AaeEiIoOuU]") {
    System.out.println("It's a vowel!");
}
 else {
   System.out.println("it's not a vowel!");
}

答案7

得分: 0

正如许多人建议的那样,您可以使用 indexOf||。我不会重复自己。您还可以使用以下方式:

public class VowelOrNot {

    public static void main(String[] args) {

        String valor = "z";

        switch (valor.toLowerCase()) {
            case "a":
            case "e":
            case "i":
            case "o":
            case "u":
                System.out.println(valor + " 是元音字母");
                break;
            default:
                System.out.println(valor + " 是辅音字母");
        }
    }
}

我在使用 switch 语句。在您希望避免多种情况下的 if-else/if-else 嵌套时,这非常方便。基本上,如果输入与任何一个 case 匹配,它就会打印该字符串是元音字母,否则是辅音字母。请注意,break 很重要。否则,它将检查默认 case 并在给定的 valor 是元音字母时打印辅音字母。

如果您希望,您可以将变量声明中的 String 替换为 char,并在 switch 语句中适当处理它。

英文:

As many suggested, you can use indexOf or ||. I'll not repeat myself. You can use something as follows as well,

public class VowelOrNot {

    public static void main(String[] args) {

        String valor = "z";

        switch (valor.toLowerCase()) {
            case "a":
            case "e":
            case "i":
            case "o":
            case "u":
                System.out.println(valor + " is vowel");
                break;
            default:
                System.out.println(valor + " is consonant");
        }
    }
}

I'm using switch case. Which is handy when you want to avoid if-else/if-if ladder in multiple scenarios. Basically, if the input is matched with any of the case then it prints that string is a vowel else it is a consonant. Note, that break is important. Otherwise it will check default case and print consonant if given valor is a vowel.

If you want, you replace String with char in the variable declaration and handle it appropriately in switch cases.

答案8

得分: 0

你可以通过使用 Java 的条件语句和逻辑 OR 运算符来轻松实现这一点。

使用短路 OR 运算符

这将更加高效,因为在检查逻辑条件时,当 Java 遇到满足条件的第一个点时,它就会返回 true。

if (valor == 'a' || valor == 'e' || valor == 'i' || valor == 'o' || valor == 'u') {
    System.out.println("xdd");
}
英文:

You can easily achieve this by using the if conditional statements with java logical OR operator.

Using short circuit OR operator

This will be more efficient because this will return true at the first point when java meets the satisfying condition while checking the logical condition.

if(valor=='a'||valor=='e'||valor=='i'||valor=='o'||valor=='u'){
    System.out.println("xdd"); 
}

huangapple
  • 本文由 发表于 2020年10月21日 00:52:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64449806.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定