Write programs that read a line of input as a string and print the positions of all vowels in the string

huangapple go评论82阅读模式
英文:

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(&quot;Enter your input: &quot;);
    String input = in.nextLine();

    int sum = 0;

    for(int i = 0; i &lt; input.length(); i++){
        char vowel = input.charAt(i);
        if(vowel == &#39;a&#39;|| vowel == &#39;e&#39;|| vowel == &#39;i&#39;||
                vowel == &#39;o&#39;|| vowel == &#39;u&#39;|| vowel == &#39;y&#39;){
            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(&quot;Enter your input: &quot;);
    String input = in.nextLine();

    int sum = 0;

    for(int i = 0; i &lt; input.length(); i++){
        char vowel = input.charAt(i);
        if(vowel == &#39;a&#39;|| vowel == &#39;e&#39;|| vowel == &#39;i&#39;||
                vowel == &#39;o&#39;|| vowel == &#39;u&#39;|| vowel == &#39;y&#39;){
            sum++;
            System.out.println(&quot;position -&gt;&quot;+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);
    }
}

Ideone.com 上运行此代码

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 = &quot;aeiouy&quot;.codePoints().sorted().toArray() ;

Get the code points of the characters in your input.

int[] codePoints = &quot;test&quot;.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 = &quot;aeiouy&quot;.codePoints().sorted().toArray();

int[] codePoints = &quot;testy&quot;.codePoints().toArray();
List &lt; Integer &gt; indicesOfVowels = new ArrayList &lt;&gt;( codePoints.length );
for ( int index = 0 ; index &lt; codePoints.length ; index++ )
{
    int position = Arrays.binarySearch( vowelCodePoints , codePoints[ index ] );
    if ( position &gt;= 0 )
    {
        String vowel = Character.toString( codePoints[ index ] );
        System.out.println( &quot;Vowel: &quot; + vowel + &quot; | Index: &quot; + index );
    }
}

See this code run at Ideone.com.

Vowel: e | Index: 1
Vowel: y | Index: 4

huangapple
  • 本文由 发表于 2020年9月12日 00:14:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63850718.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定