Java 使用for循环检查数组

Java 使用for循环检查数组,java,arrays,for-loop,Java,Arrays,For Loop,我试图使用for循环检查数组中的每个字符,并打印字符,它在数组中的位置,以及它是什么类型的字符(元音、辅音等)。到目前为止,我有: char[] myName = new char[] {'J', 'o', 'h', 'n', ' ', 'D', 'o', 'e'}; System.out.print("\nMy name is: "); for(int index=0; index < myName.length ; index++)

我试图使用for循环检查数组中的每个字符,并打印字符,它在数组中的位置,以及它是什么类型的字符(元音、辅音等)。到目前为止,我有:

char[] myName = new char[] {'J', 'o', 'h', 'n', ' ', 'D', 'o', 'e'};

         System.out.print("\nMy name is: ");

            for(int index=0; index < myName.length ; index++)
            System.out.print(myName[index]);

            for(char c : myName) {
            if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
            {
                System.out.println("The character located at position is a vowel.");
            }
            else if (c == 'j' || c == 'h' || c == 'n' || c == 'd')
            {
                System.out.println("The character located at position is a consonant.");
            }
            else if (c == ' ')
            {
                System.out.println("The character located at position is a space.");
            }
char[]myName=new char[]{'J','o','h','n','D','o','e'};
System.out.print(“\n我的名字是:”);
for(int index=0;index

如何打印字符的位置(即“位于x位置的字符x是元音”)

您的思路正确。循环正常,但如果您实际上不需要索引,请尝试使用
foreach
语法,如下所示:

 char[] myName = new char[] {'J', 'o', 'h', 'n', ' ', 'D', 'o', 'e'};

 System.out.print("\nMy name is: ");

 for(char c : myName) {
     System.out.print(c); 
 }
现在加入一些逻辑:

 int i = 0;
 for(char c : myName) {
     i++;
     // Is the char a vowel?
     if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
         // do something - eg print in uppercase
         System.out.print(Character.toUpperCase(c) + " at position " + i);
     } else {
         // do something else - eg print in lowercase
         System.out.print(Character.toLowerCase(c) + " at position " + i);
     }
 }
你必须弄清楚你想在这里做什么。现在开始做:)

编辑:以显示位置的使用,这有点笨拙,但代码仍然少于循环的标准提示:

  • 您应该为当前使用的循环使用
    类型。值索引变量在您的输出中将非常有用

  • Character类有许多方法用于对字符进行分类,以及从大写转换为小写,反之亦然

  • 您还可以使用
    ==
    测试字符

  • 您还可以使用switch语句来区分不同种类的字母,并使用
    default
    分支来表示其余的字母

  • char[]myName=新字符[]{'J','o','h','n','D','o','e'};
    System.out.print(“\n我的名字是:”);
    for(int index=0;index
    您的字母有什么问题?您是否在寻找一种方法来确定当前正在检查的字母是元音还是辅音?看起来你的思路是对的,但你需要想出一种方法来确定这一部分。我不确定如何让它打印出它是什么类型的字符。只是一个小小的补充,如果你真的在处理字符,那么就选择切换大小写。谢谢。这真的很有帮助。还有一个问题。。。如何打印字符的位置(即“位于x位置的字符x是元音”)@JDB-这就是为什么每个循环的
    在这个特定示例中不合适的原因。@Steven Oh真的吗???检查编辑-它有一个共同的模式来解决这个问题,@Sandy-switch更难阅读,并创建更多的代码;带着开关和所有的坏消息。您还没有处理大写字母,也没有对非字母和非空格的字符进行分类。你需要走多远取决于问题的确切表述方式;i、 e.允许您对输入字符做出哪些假设。现在,您还应该能够在输出中包含该位置。
    
         char[] myName = new char[] {'J', 'o', 'h', 'n', ' ', 'D', 'o', 'e'};
    
         System.out.print("\nMy name is: ");
    
            for(int index=0; index < myName.length ; index++)
            char c = myname[index];
            if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
            {
                System.out.println("The character " + c + "located at position " + index + " is a vowel.");
            }
        ... }