英文:
Write programs that read a line of input as a string and print the positions of all vowels in the string
问题
我是Java的初学者,关于循环
我有一个问题。我一直在处理这个任务,任务要求:编写程序,将输入的一行作为字符串
读取并打印字符串中所有元音字母
的位置
。
我已经成功打印出输入中的元音字母数量,但在打印它们的位置时遇到了困难。
任何帮助将不胜感激!
System.out.println("输入您的内容:");
String input = in.nextLine();
int sum = 0;
for (int i = 0; i < input.length(); i++) {
char vowel = input.charAt(i);
if (vowel == 'a' || vowel == 'e' || vowel == 'i' ||
vowel == 'o' || vowel == 'u' || vowel == 'y') {
sum++;
}
}
System.out.println(sum);
英文:
I'm a beginner in Java and I have a question regarding loops
. I've been struggling with this task where it says: Write programs that read a line of input as a string
and print the positions
of all vowels
in the string.
I have managed to print out the number of vowels in the input but I got stuck when it came to print out their positions.
Any help would be appreciated!
System.out.println("Enter your input: ");
String input = in.nextLine();
int sum = 0;
for(int i = 0; i < input.length(); i++){
char vowel = input.charAt(i);
if(vowel == 'a'|| vowel == 'e'|| vowel == 'i'||
vowel == 'o'|| vowel == 'u'|| vowel == 'y'){
sum++;
}
}
System.out.println(sum);
答案1
得分: 4
System.out.println("请输入内容:");
String input = in.nextLine();
int sum = 0;
for (int i = 0; i < input.length(); i++) {
char vowel = input.charAt(i);
if (vowel == 'a' || vowel == 'e' || vowel == 'i' ||
vowel == 'o' || vowel == 'u' || vowel == 'y') {
sum++;
System.out.println("位置 -> " + i); //添加的行
}
}
System.out.println(sum);
英文:
System.out.println("Enter your input: ");
String input = in.nextLine();
int sum = 0;
for(int i = 0; i < input.length(); i++){
char vowel = input.charAt(i);
if(vowel == 'a'|| vowel == 'e'|| vowel == 'i'||
vowel == 'o'|| vowel == 'u'|| vowel == 'y'){
sum++;
System.out.println("position ->"+i);//added line
}
}
System.out.println(sum);
you just needed to add single line that prints the positions
答案2
得分: 0
避免使用 char
自 Java 2 起,char
类型实际上就已经存在问题,而自 Java 5 起就成为遗留。作为一个 16 位值,char
无法表示 Unicode 定义的大多数 144,000 多个字符。
代码点
要处理单个字符,使用 代码点 整数数字。
定义你的目标元音字符集。对数组进行排序,以便我们可以通过二分查找进行快速搜索。
int[] vowelCodePoints = "aeiouy".codePoints().sorted().toArray();
获取输入中字符的代码点。
int[] codePoints = "test".codePoints().toArray();
循环遍历我们输入字符串的代码点数组,按索引(从零开始计数)进行。测试每个代码点,以查看是否在元音代码点数组中找到。
要进行反向操作,即从代码点整数转换为文本字符,请调用 Character.toString
。
示例代码
将所有代码整合在一起。
int[] vowelCodePoints = "aeiouy".codePoints().sorted().toArray();
int[] codePoints = "testy".codePoints().toArray();
List<Integer> indicesOfVowels = new ArrayList<>(codePoints.length);
for (int index = 0; index < codePoints.length; index++) {
int position = Arrays.binarySearch(vowelCodePoints, codePoints[index]);
if (position >= 0) {
String vowel = Character.toString(codePoints[index]);
System.out.println("Vowel: " + vowel + " | Index: " + index);
}
}
Vowel: e | Index: 1
Vowel: y | Index: 4
英文:
Avoid char
The char
type has been essentially broken since Java 2, and legacy since Java 5. As a 16-bit value, char
is physically incapable of representing most of the >144,000 characters defined in Unicode.
Code point
To work with individual characters, use code point integer numbers.
Define your set of targeted vowels. Sort the array, so that we may search quickly via binary search.
int[] vowelCodePoints = "aeiouy".codePoints().sorted().toArray() ;
Get the code points of the characters in your input.
int[] codePoints = "test".codePoints().toArray() ;
Loop the array of code points of our input string, by index (zero-based counting). Test each code point to see if it is found within our array of vowel code points.
To go in the reverse direction, from code point integer to the character as text, call Character.toString
.
Example code
Pull all the code together.
int[] vowelCodePoints = "aeiouy".codePoints().sorted().toArray();
int[] codePoints = "testy".codePoints().toArray();
List < Integer > indicesOfVowels = new ArrayList <>( codePoints.length );
for ( int index = 0 ; index < codePoints.length ; index++ )
{
int position = Arrays.binarySearch( vowelCodePoints , codePoints[ index ] );
if ( position >= 0 )
{
String vowel = Character.toString( codePoints[ index ] );
System.out.println( "Vowel: " + vowel + " | Index: " + index );
}
}
See this code run at Ideone.com.
Vowel: e | Index: 1
Vowel: y | Index: 4
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论