如何使用while循环计算字符串中的特定字符 - Java

时间:2017-06-27 12:22:18

标签: java

我对此非常陌生,虽然我能够通过for循环执行此操作,但分配需要一个while循环。我尝试了以下不能正常工作的方法。请帮忙!

package charcounter;

import java.util.Scanner;

public class CharCounter {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        char userChar = '0';
        String inputEntry = "";
        String inputCharacter = "";
        int foundOccurrences = 0;

        System.out.print("Please enter one or more words: ");
        inputEntry = in.nextLine();

        System.out.print("\nPlease enter one character: ");
        inputCharacter = in.next();
        userChar = inputCharacter.charAt(0);

        while (foundOccurrences < inputEntry.length()) {
            if (userChar == inputEntry.charAt(0)) {

            }

            System.out.println("There is " + foundOccurrences + " occurrence(s) of " + inputCharacter + " in test.");

            foundOccurrences++;
        }

    }

}

4 个答案:

答案 0 :(得分:2)

这样的事情:

        int i = 0;
        while (i < inputEntry.length()) {
            if (userChar == inputEntry.charAt(i++)) {
                foundOccurrences++;
            }
        }
        System.out.println("There is " + foundOccurrences + " occurrence(s) of " + inputCharacter + " in test.");

修正了错误

答案 1 :(得分:1)

你真的应该尝试调试你的程序并尝试找一个你作为程序员从中获利很多的解决方案。你的代码中有两个问题 1.您始终在输入文本中测试相同的字符 2.您的foundOccurrences变量不会像出现计数器那样使用,而是如果在文本中找到或没有找到字母,则它会增加,这对您来说是一个简单的解决方案:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    char userChar = '0';
    String inputEntry = "";
    String inputCharacter = "";
    int foundOccurrences = 0;

    System.out.print("Please enter one or more words: ");
    inputEntry = in.nextLine();

    System.out.print("\nPlease enter one character: ");
    inputCharacter = in.next();
    userChar = inputCharacter.charAt(0);

    int index = 0;
    while (index < inputEntry.length()) {
        if (userChar == inputEntry.charAt(index)) {
            foundOccurrences++;
        }
        index++;
    }

    System.out.println("There is " + foundOccurrences + " occurrence(s) of " + inputCharacter + " in test.");
}

答案 2 :(得分:0)

您可以使用索引变量i来帮助您逐个查看字符串的字符,以便检查相等性。 类似的东西:

while(i&lt; inputEntry.length()){ ... 我++; }

答案 3 :(得分:0)

一个保持循环计数器的索引,用于迭代字符串和
foundOccurrences计数器,用于在字符串中找到给定字符时保持计数。

从双方检查的另一种方法。

int start =0;
        int end=inputEntry.length()-1;
        while (start  < end) {
            if (userChar == inputEntry.charAt(start)) {
                foundOccurrences++;
            }
            if (userChar == inputEntry.charAt(end)) {
                foundOccurrences++;
            }
            start++;
            end--;
        }

BUG修复:

public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            char userChar = '0';
            String inputEntry = "";
            String inputCharacter = "";
            int foundOccurrences = 0;

            System.out.print("Please enter one or more words: ");
            inputEntry = in.nextLine();

            System.out.print("\nPlease enter one character: ");
            inputCharacter = in.next();
            userChar = inputCharacter.charAt(0);
            int index = 0;
            while (index < inputEntry.length()) {
                if (userChar == inputEntry.charAt(index)) {
                    foundOccurrences++;
                }
                index++;
            }
            System.out.println("There is " + foundOccurrences + " occurrence(s) of " + inputCharacter + " in test.");
        }