如何将此FOR循环转换为WHILE循环,以便它也计入辅音? (JAVA)

时间:2018-07-30 06:48:36

标签: java loops for-loop while-loop count

如何将此FOR循环转换为WHILE循环,以便它也计入辅音?到目前为止,我知道如何使用While循环做一个基本的计数器...

import java.util.Scanner;

public class VowelsCount {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Input a string: ");
        String str = in.nextLine();

        System.out.print("Number of Vowels in the string: " + vowCount(str) + "\n");
    }

    public static int vowCount(String str) {
        int count = 0;
        for (int index = 0; index < str.length(); index++) {
            if (str.charAt(index) == 'a' || str.charAt(index) == 'e' ||
                str.charAt(index) == 'i' || str.charAt(index) == 'o' ||
                str.charAt(index) == 'u') {
                count++;
            }
        }
        return count;
    }
}

6 个答案:

答案 0 :(得分:2)

while()循环的参数为termination statement,这意味着除了初始化和递增索引外,还必须将终止设置为while参数。 编辑,必须在循环之前初始化index变量。

您的情况:

while(index != str.length()){
     //rest of the statement, those from your for loop
     index++;
}

答案 1 :(得分:2)

这是现成的LetterCount类,可以封装所有glogic进行字母计数:

public final class LetterCount {

    private final String str;
    private final int vowel;
    private final int consonant;

    public static LetterCount create(String str) {
        int[] count = count(str);
        return new LetterCount(str, count[0], count[1]);
    }

    private static int[] count(String str) {
        int i = str.length();
        int[] count = new int[2];

        while (--i >= 0) {
            char ch = Character.toLowerCase(str.charAt(i));

            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
                count[0]++;
            else if (ch >= 'a' && ch <= 'z')
                count[1]++;
        }

        return count;
    }

    private LetterCount(String str, int vowel, int consonant) {
        this.str = str;
        this.vowel = vowel;
        this.consonant = consonant;
    }

    public String getStr() {
        return str;
    }

    public int getVowel() {
        return vowel;
    }

    public int getConsonant() {
        return consonant;
    }

}

答案 2 :(得分:1)

您可以通过初始化另一个变量来计数辅音,如果不满足条件,则在else条件下将其加1。 对于while循环,您可以执行以下操作。

int countVow = 0;
int index = 0;
while(index != str.length())
{
    if(vowel)
    {
        countVow++;
    }

    index++
}
int countCon = index = countVow;

答案 3 :(得分:1)

这是一个函数count,它使用while循环来计数辅音和元音(尽管循环的类型并不重要):

import java.util.Scanner;

public class VowelsCount {
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        Scanner in = new Scanner(System.in);

        System.out.print("Input a string: ");
        String str = in.nextLine();

        Counts counts = count(str);

        System.out.println("Number of Vowels in the string: " + counts.getVowels());
        System.out.println("Number of Consonants in the string: " + counts.getConsonants());
    }

    public static Counts count(String str) {
        int vowCount = 0;
        int consCount = 0;

        str = str.toLowerCase();

        int i = str.length();
        while(i-- > 0) {
            char ch = str.charAt(i);
            if(ch >= 'a' && ch <= 'z') {
                switch(ch) {
                    case 'a':
                    case 'e':
                    case 'i':
                    case 'o':
                    case 'u':
                        vowCount++;
                        break;
                    default:
                        consCount++;
                        break;
                }
            }
        }

        return new Counts(vowCount, consCount);
    }

    public static final class Counts {
        private int vowels;
        private int consonants;

        public Counts(int vowels, int consonants) {
            this.vowels = vowels;
            this.consonants = consonants;
        }

        public int getVowels() {
            return vowels;
        }

        public int getConsonants() {
            return consonants;
        }
    }
}

该函数的工作原理基本上是:循环遍历字符串中的所有字符(反之),然后首先检查它是否为字母(在az之间),以及如果是,则它必须是元音或辅音。因此,我们只需要检查哪一个并增加相应的计数器即可。

答案 4 :(得分:0)

这工作正常:-

public static int vowCount(String str) {
            int count = 0;
            int index = 0;
            while (index < str.length()) {
                if (str.charAt(index) == 'a' || str.charAt(index) == 'e' || str.charAt(index) == 'i' || str.charAt(index) == 'o' || str.charAt(index) == 'u') {
                    count++;
                }
                index++;
            }
            return count;
        }

答案 5 :(得分:0)

for代替while也不需要计算辅音。
两者都可以做到这一点,并且可以更好地设计一个for进行迭代,直到特定索引对您的用例而言更好为止。

这个想法是定义一个元音字符串和一个辅音字符串,并针对每个遇到的字符,根据这些String的匹配来增加计数器。

static final String vowels = "aei...";
static final String consonants = "bcdf...";

public static int[] vowCount(String str){
  int countV = 0;
  int countC = 0;
  for (int index = 0; index < str.length(); index++){
    String currentChar = str.substring(index, index+1);
    if (vowels.contains(currentChar)){
     countV++;
    }
    else if (consonants.contains(currentChar)){
     countC++;
    }
  }

  return new int[]{countV, countC};
}

我返回了一个int[]数组,但是您也可以返回一个Count类的实例,您可以定义该类来保存这两个数据。