Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用while循环计算字符串中的特定字符-Java_Java - Fatal编程技术网

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

如何使用while循环计算字符串中的特定字符-Java,java,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 use

我对这个非常陌生,虽然我能够用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++;
        }

    }

}
packagecharcounter;
导入java.util.Scanner;
公共类字符计数器{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
char userChar='0';
字符串inpuntery=“”;
字符串inputCharacter=“”;
int=0;
System.out.print(“请输入一个或多个单词:”);
inpuntery=in.nextLine();
System.out.print(“\n请输入一个字符:”);
inputCharacter=in.next();
userChar=inputCharacter.charAt(0);
while(foundoccurrents
类似这样的内容:

        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.");
inti=0;
而(i
修复了类似这样的错误:

        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.");
inti=0;
而(i

修复了这个错误

你真的应该试着调试你的程序,试着找到一个解决方案,作为一名程序员,你从中获益匪浅 1.您总是在输入文本中测试相同的字符 2.FoundOccurrencess变量的使用方式与事件计数器不同,相反,如果在文本中找到或未找到字母,则该变量将递增。以下是一个简单的解决方案:

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.");
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
char userChar='0';
字符串inpuntery=“”;
字符串inputCharacter=“”;
int=0;
System.out.print(“请输入一个或多个单词:”);
inpuntery=in.nextLine();
System.out.print(“\n请输入一个字符:”);
inputCharacter=in.next();
userChar=inputCharacter.charAt(0);
int指数=0;
while(索引
作为一名程序员,你真的应该试着调试你的程序,试着找到一个解决方案。你的代码中有两个问题 1.您总是在输入文本中测试相同的字符 2.FoundOccurrencess变量的使用方式与事件计数器不同,相反,如果在文本中找到或未找到字母,则该变量将递增。以下是一个简单的解决方案:

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.");
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
char userChar='0';
字符串inpuntery=“”;
字符串inputCharacter=“”;
int=0;
System.out.print(“请输入一个或多个单词:”);
inpuntery=in.nextLine();
System.out.print(“\n请输入一个字符:”);
inputCharacter=in.next();
userChar=inputCharacter.charAt(0);
int指数=0;
while(索引
您可以使用索引变量i,它将帮助您逐个检查字符串的字符,以检查是否相等。 比如:

而(i}

您可以使用索引变量i,它将帮助您逐个检查字符串的字符,以检查是否相等。 比如:

而(i}

一个索引,用于保存循环计数器,以便遍历字符串和
FoundOccessions计数器,用于在字符串中找到给定字符时保持计数

另一种从两侧检查的方法

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--;
        }
int start=0;
int end=inpuntery.length()-1;
while(开始<结束){
if(userChar==inpuntery.charAt(start)){
found++;
}
if(userChar==inpuntery.charAt(end)){
found++;
}
启动++;
结束--;
}
错误修复:

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.");
        }
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
char userChar='0';
字符串inpuntery=“”;
字符串inputCharacter=“”;
int=0;
System.out.print(“请输入一个或多个单词:”);
inpuntery=in.nextLine();
System.out.print(“\n请输入一个字符:”);
inputCharacter=in.next();
userChar=inputCharacter.charAt(0);
int指数=0;
while(索引
一个索引,用于保存循环计数器以循环字符串和
使用计数器来保持计数