我是Java的初学者,我正在学习本 class ,对此需要一些帮助。基本上,用户将输入一个字符串,然后程序将仅在一行上打印出元音。

import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner inp = new Scanner(System.in);
    System.out.print("In:");
    String word = inp.nextLine();
    //write your code below
    for(int whatsat = 0; whatsat < word.length(); whatsat++){
      if (word.charAt(whatsat).equals("a")){  //how to declare mutiple letters?
        System.out.print(word.charAt(whatsat));
      }

  }
}
}

最佳答案

我同意@Logan。您无需使用equals()比较原始类型值(int,char,boolean等),而只需使用简单的==表达式即可。

import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner inp = new Scanner(System.in);
    System.out.print("In:");
    String word = inp.nextLine();
    //write your code below
    for(int whatsat = 0; whatsat < word.length(); whatsat++){
      char c = Character.toLowerCase(word.charAt(whatsat));
      if (c == 'a' || c == 'e'|| c == 'i' || c == 'o' || c == 'u'){
        System.out.print(word.charAt(whatsat));
      }

  }
}
}

09-13 12:02