这就是我所拥有的
import java.util.Scanner;
public class A9 {
public static String getPhrase () {
Scanner input = new Scanner(System.in);
System.out.print ("Enter a phrase: ");
String phrase = input.nextLine();
return phrase;
}
public static void reportPhrase (String phrase) {
System.out.println (phrase);
}
public static void printHistogram (String phrase) {
for(int i=0;i <phrase.length();i++){
if((phrase.charAt(i) == 'a') ||
(phrase.charAt(i) == 'e') ||
(phrase.charAt(i) == 'i') ||
(phrase.charAt(i) == 'o') ||
(phrase.charAt(i) == 'u')) {
System.out.println(phrase.charAt(i) + ": ");
for (int j = 0; j < phrase.length(); j++) {
System.out.print("*");
}
public static void main(String args[]) {
String phrase;
phrase = getPhrase ();
reportPhrase (phrase);
printHistogram (phrase);
// Call the method printHistogram here
}
}
,输出就是这个
Enter a phrase: frederator
frederator
e:
**********e:
**********a:
**********o:
**********
有人可以重写它以使输出更像这样吗?
ex " Alphabet soup is my favorite soup in the whole world."
a: * * *
e: * * * *
i: * * *
o: * * * * *
u: * *
答案 0 :(得分:0)
import java.util.Scanner;
public class A9 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a phrase: ");
String str = in.nextLine();
printVowels(str);
}
public static void printVowels(String str) {
String a = "";
String e = "";
String i = "";
String o = "";
String u = "";
for (int ind = 0; ind < str.length(); ind++) {
switch (str.charAt(ind)) {
case 'a':
case 'A':
a += "* ";
break;
case 'e':
case 'E':
e += "* ";
break;
case 'i':
case 'I':
i += "* ";
break;
case 'o':
case 'O':
o += "* ";
break;
case 'u':
case 'U':
u += "* ";
break;
}
}
System.out.println("a: " + a);
System.out.println("e: " + e);
System.out.println("i: " + i);
System.out.println("o: " + o);
System.out.println("u: " + u);
}
}
答案 1 :(得分:0)
希望这会有所帮助。我用输入创建了一个字符串,但是你可以用你的方法加入它。
public class Te {
public static void main(String[] args) {
String s="Alphabet soup is my favorite soup in the whole world.";
int arr[] = new int[5];
String S = s.toUpperCase ();
arr[0]=S.length() - S.replace("A", "").length();
arr[1]=S.length() - S.replace("E", "").length();
arr[2]=S.length() - S.replace("I", "").length();
arr[3]=S.length() - S.replace("O", "").length();
arr[4]=S.length() - S.replace("U", "").length();
System.out.print("a: ");printStar(arr[0]);
System.out.print("e: ");printStar(arr[1]);
System.out.print("i: ");printStar(arr[2]);
System.out.print("o: ");printStar(arr[3]);
System.out.print("u: ");printStar(arr[4]);
}
public static void printStar(int n){
if(n > 0){
System.out.print("*");
printStar(n-1);
}
if (n==0)
{System.out.println("");}
}
}
答案 2 :(得分:0)
使用Hammad2506的代码和OP提供的基本代码我使用其他方法中的现有字符串准确地描述了OP正在寻找的内容。请原谅我可怕的空白。
/**
* A simple program that prompts the user
* for a line of text.
*/
import java.util.Scanner;
public class Loops {
public static String getPhrase () {
Scanner input = new Scanner(System.in);
System.out.print ("Enter a phrase: ");
String phrase = input.nextLine();
return phrase;
}
public static void reportPhrase (String phrase) {
System.out.println (phrase);
}
public static void printHistogram (String phrase) {
// Your code here
int arr[] = new int[5];
String P = phrase.toUpperCase ();
arr[0]=P.length() - P.replace("A", "").length();
arr[1]=P.length() - P.replace("E", "").length();
arr[2]=P.length() - P.replace("I", "").length();
arr[3]=P.length() - P.replace("O", "").length();
arr[4]=P.length() - P.replace("U", "").length();
System.out.print("a: ");printStar(arr[0]);
System.out.print("e: ");printStar(arr[1]);
System.out.print("i: ");printStar(arr[2]);
System.out.print("o: ");printStar(arr[3]);
System.out.print("u: ");printStar(arr[4]);
}
public static void printStar(int n){
if(n > 0){
System.out.print("*");
printStar(n-1);
}
if (n==0)
{System.out.println("");
}
}
public static void main(String args[]) {
String phrase;
phrase = getPhrase ();
reportPhrase (phrase);
// Call the method printHistogram here
printHistogram (phrase);
}
}