對於學校我必須製作一個掃描器,它能夠接受字符串輸入並計算1「a」等元音的數量。我的問題是,當我輸入一個字符串時,它不會經過循環,我可以連續輸入和輸入什麼都不做。 (我使用BlueJ來編碼)。謝謝你的幫助!Java代碼沒有處理輸入
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.
}
}
你永遠只要求輸入一次......也使用字符串的equals方法Java中 –
比較字符串時是否有您使用的環路長度<1的原因嗎?這不會遍歷整個字符數。也可以看看使用開關盒 –