卡在开关循环中
Stuck in a switch loop
我写了一个作业来尝试计算字符串中每种元音的数量。
它编译得很好,但似乎在 switch 语句上循环,尽管谷歌搜索了大约一个小时,但我看不出我做错了什么。
请帮忙! =]
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));
}
}
}
删除分号
while (position < input.length());
^
这会阻止 position
递增
你多了一个不该有的分号。
while (position < input.length());
这个分号不是语法错误。它描述了一个带有空循环内容的 while 块。它转换为:
while (position < input.length()) {}
所以,去掉分号,一切都很好。
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));
}
}
试试这个。
正如其他人所提到的,你有一个不属于它的分号。
我还纠正了你的逻辑并将打印语句移到循环之外。
希望这就是您所期望的最终结果
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
我写了一个作业来尝试计算字符串中每种元音的数量。
它编译得很好,但似乎在 switch 语句上循环,尽管谷歌搜索了大约一个小时,但我看不出我做错了什么。
请帮忙! =]
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));
}
}
}
删除分号
while (position < input.length());
^
这会阻止 position
递增
你多了一个不该有的分号。
while (position < input.length());
这个分号不是语法错误。它描述了一个带有空循环内容的 while 块。它转换为:
while (position < input.length()) {}
所以,去掉分号,一切都很好。
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));
}
}
试试这个。
正如其他人所提到的,你有一个不属于它的分号。 我还纠正了你的逻辑并将打印语句移到循环之外。
希望这就是您所期望的最终结果
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