我已经写了一个作业,试着计算一个字符串中每种元音的数字。
它编译得很好,但似乎在循环语句中循环,我无法看到我做错了什么,尽管大约一个小时的谷歌搜索。
请帮忙! =]
import java.util.Scanner;
public class assignment3b
{
public static void main(String[] args)
{
int alpha=0, epsilon=0, india=0, oscar=0, uniform=0, position=0, length;
String input;
char letter;
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to the vowel parse-o-matic");
System.out.println("\nThis program will count all lower case vowels in whatever you type.");
System.out.print("\nPlease enter the word you'd like to have parsed : ");
input = scan.next();
System.out.println("\n\nThe word " + input + " has: "); // reprints the word before stripping spaces
input = input.replaceAll("\\s+",""); // Removes whitespace so they don't get counted.
while (position < input.length());
{
letter = input.charAt(position);
switch (letter)
{
case 'a':
alpha = alpha + 1;
position = position +1;
break;
case 'e':
epsilon = epsilon + 1;
position = position +1;
break;
case 'i':
india = india + 1;
position = position +1;
break;
case 'o':
oscar = oscar + 1;
position = position +1;
break;
case 'u':
uniform = uniform + 1;
position = position +1;
break;
default:
position++;
break;
}
System.out.println("a's = " + alpha);
System.out.println("e's = " + epsilon);
System.out.println("i's = " + india);
System.out.println("o's = " + oscar);
System.out.println("u's = " + uniform);
System.out.println("\nOther characters = " + (input.length() - alpha -epsilon - india -oscar - uniform));
}
}
}
答案 0 :(得分:6)
删除分号
while (position < input.length());
^
阻止position
递增
答案 1 :(得分:2)
import java.util.Scanner;
public class assignment3b
{
public static void main(String[] args)
{
int alpha = 0, epsilon = 0, india = 0, oscar = 0, uniform = 0, position = 0, length;
String input;
char letter;
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to the vowel parse-o-matic");
System.out.println("\nThis program will count all lower case vowels in whatever you type.");
System.out.print("\nPlease enter the word you'd like to have parsed : ");
input = scan.next();
System.out.println("\n\nThe word " + input + " has: "); // reprints the word before
// stripping spaces
input = input.replaceAll("\\s+", ""); // Removes whitespace so they don't get counted.
int len = input.length();
while (position < len)
{
letter = input.charAt(position);
switch (letter)
{
case 'a':
alpha = alpha + 1;
position = position + 1;
break;
case 'e':
epsilon = epsilon + 1;
position = position + 1;
break;
case 'i':
india = india + 1;
position = position + 1;
break;
case 'o':
oscar = oscar + 1;
position = position + 1;
break;
case 'u':
uniform = uniform + 1;
position = position + 1;
break;
default:
position++;
break;
}
}
System.out.println("a's = " + alpha);
System.out.println("e's = " + epsilon);
System.out.println("i's = " + india);
System.out.println("o's = " + oscar);
System.out.println("u's = " + uniform);
System.out.println("\nOther characters = "
+ (input.length() - alpha - epsilon - india - oscar - uniform));
}
}
试试这个。
正如其他人所提到的,你有一个它不属于的分号。 而且我还纠正了你的逻辑并将print语句移到了循环之外。
希望这是你期望的最终结果
Welcome to the vowel parse-o-matic
This program will count all lower case vowels in whatever you type.
Please enter the word you'd like to have parsed : striker
The word striker has:
a's = 0
e's = 1
i's = 1
o's = 0
u's = 0
Other characters = 5
答案 2 :(得分:0)
你有一个不属于它的额外半冒号。
while (position < input.length());
此分号不是语法错误。它描述了一个带有空循环内容的while块。它转换为:
while (position < input.length()) {}
所以,放下分号,你们都很好。