【发布时间】:2016-11-28 23:50:04
【问题描述】:
对于学校,我必须制作一个扫描仪,它能够接受字符串输入并计算元音的数量,如 1“a”等。我的问题是,当我输入一个字符串时,它不会通过循环和我可以连续输入并输入什么都不做。 (顺便说一下,我正在使用 BlueJ 进行编码)。感谢您的帮助!
package VowelCounter;
/**
* Description:
* Author: Jack Cannon
* Date: 11/15/16
*/
import java.util.Scanner;
public class VowelCounter
{
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
//Prompt the user to enter a phrase
System.out.println("Type in a sentence.");
String phrase = input.nextLine();
//as long as the string isn't "quit",
//count the vowels in the string.
int length = phrase.length();
while(phrase != "quit")
{
int a = 0;
int e = 0;
int i = 0;
int o = 0;
int u = 0;
for(int c = 0; length < 1; length++)
{
char vowels = phrase.charAt(c);
if(vowels == 'a')
{
}
else if(vowels == 'e')
{
}
else if(vowels == 'i')
{
}
else if(vowels == 'o')
{
}
else if(vowels == 'u')
{
}
}
}
System.out.println("There are " + 'a' + "a's");
System.out.println("There are " + 'e' + "e's");
System.out.println("There are " + 'i' + "i's");
System.out.println("There are " + 'o' + "o's");
System.out.println("There are " + 'u' + "u's");
//print out the count of the vowels for this string.
//prompt the user for another phrase.
}
}
【问题讨论】:
-
您只要求输入一次...在比较 java 中的字符串时也使用字符串 equals 方法
-
你有什么理由在循环中使用 length