找到元音并打印它们更简单的方法?JAVA

找到元音并打印它们更简单的方法?JAVA,java,for-loop,Java,For Loop,嘿,这是我第一次发帖!我让我的程序从用户的输入中打印出元音,但我觉得我在for循环中重复了很多。有没有更快的方法?这段代码是否可读,格式是否正确 import java.util.Scanner; public class Task09 { public static void main(String[] args) { Scanner input = new Scanner(System.in); String vowels =""; //input from

嘿,这是我第一次发帖!我让我的程序从用户的输入中打印出元音,但我觉得我在for循环中重复了很多。有没有更快的方法?这段代码是否可读,格式是否正确

import java.util.Scanner;
public class Task09 {

public static void main(String[] args) 
{

    Scanner input = new Scanner(System.in);

    String vowels ="";

    //input from user
    String answer= input.next()

    //loop to find vowels

    for(int i = 0 ;i<answer.length();i++)
    {
        char answerPosition = answer.charAt(i);

        //checks if there are vowels in code
        if (answerPosition =='a'
                ||answerPosition  =='e'
                ||answerPosition  =='i'
                ||answerPosition =='o'
                ||answerPosition =='u'
                ||answerPosition =='A'
                ||answerPosition =='I'
                ||answerPosition =='O'
                ||answerPosition =='U')
        {
            vowels += answerPosition + " ";
        }

    }
            System.out.println("The vowels are:" + vowels);

    input.close();

}
import java.util.Scanner;
公开课任务09{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
字符串元音=”;
//用户输入
String answer=input.next()
//循环查找元音
对于(int i=0;i请尝试以下方法:

  String newString = answer.replaceAll("[^AaeEiIoOuU]", "");
  System.out.println(newString);
您也不需要循环,您的代码将紧凑而甜美。

您可以:

if ( "aeiouAEIOU".indexOf(answerPosition) >= 0 ) {
    vowels += answerPosition + " ";
}
在循环内部

此外,就风格而言,您可能会以稍微不同的方式编写迭代:

for (char c: answer.toCharArray()) {
   if ( "aeiouAEIOU".indexOf(c) >= 0 ) {
      vowels += c + " ";
   }
}

你也可以这样做

import java.util.Scanner;

public class Hi {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        String vowels = "";

        // input from user
        String answer = input.next();

        // loop to find vowels

        for (int i = 0; i < answer.length(); i++) {
            char answerPosition = answer.charAt(i);
            char tempAnsPos = Character.toUpperCase(answer.charAt(i));

            // checks if there are vowels in code
            if (tempAnsPos == 'A' || tempAnsPos == 'E' || tempAnsPos == 'I' || tempAnsPos == 'O' || tempAnsPos == 'U') {
                vowels += answerPosition + " ";
            }

        }
        System.out.println("The vowels are:" + vowels);

        input.close();

    }
}
import java.util.Scanner;
公共类Hi{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
字符串元音=”;
//用户输入
字符串answer=input.next();
//循环查找元音
for(int i=0;i
你的代码行得通吗?不,这不是最干净的代码,但对新手来说也不错。如果它对你行得通,我会说随它去。你的代码没有问题(除了
输入后缺少分号。下一步
。既然您正在寻找改进工作代码的方法,那么请将您的问题发布在上,确保您可以使用奇特的正则表达式或其他方法来做同样的事情。但是对于这个小示例,这是可以的。您可以在数组中标记元音。然后,您的代码变小感谢大家的支持。)e快速输入。我希望有一天能为您做出贡献lol:D@Drejc:该死的复制粘贴:)。更正我喜欢它!太酷了,几乎有一种方法可以解决所有问题!对于正则表达式,[^]的作用是什么?它们与数组有关吗?该方法是否将元音替换为“”?这将如何让我打印出元音?它在java网站上说:“参数:regex-此字符串要匹配的正则表达式替换-每个匹配要替换的字符串”这意味着不包含volwels(^表示不在)。非常感谢您的快速回复