Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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
Java 计数事件_Java - Fatal编程技术网

Java 计数事件

Java 计数事件,java,Java,我正在尝试编写一个代码来计算另一个字符串中1个字符串的出现次数。因此,如果用户输入hello,然后输入e,那么代码应该表示出现了1次e。然而,我的电流执行一个无限循环 package charcounter; import java.util.Scanner; public class CharCounter { public static void main(String[] args) { Scanner scnr = new Scanner(System.i

我正在尝试编写一个代码来计算另一个字符串中1个字符串的出现次数。因此,如果用户输入hello,然后输入e,那么代码应该表示出现了1次e。然而,我的电流执行一个无限循环

package charcounter;

import java.util.Scanner;
public class CharCounter {


    public static void main(String[] args) {

        Scanner scnr = new Scanner(System.in);
        String inputEntry;
        String inputCharacter;

        System.out.println("Please enter a multi word string: ");
        inputEntry = scnr.nextLine();

        System.out.println("Enter another string: ");
        inputCharacter = scnr.nextLine();

        if (inputCharacter.length() == 1){
            while (inputEntry.contains(inputCharacter)){
                int occurrences = 0;
                for(occurrences = 0;inputEntry.contains(inputCharacter); occurrences++ ){
                    System.out.println("There is " + occurrences + " of " + inputCharacter);
                }
            }
        }
        else{
            System.out.println("Your string is too long.");
        }
    }
}
我尝试将for循环的条件更改为inpuntery.equalInputCharacter,但也有一个无限循环

package charcounter;

import java.util.Scanner;
public class CharCounter {


    public static void main(String[] args) {

        Scanner scnr = new Scanner(System.in);
        String inputEntry;
        String inputCharacter;

        System.out.println("Please enter a multi word string: ");
        inputEntry = scnr.nextLine();

        System.out.println("Enter another string: ");
        inputCharacter = scnr.nextLine();

        if (inputCharacter.length() == 1){
            while (inputEntry.contains(inputCharacter)){
                int occurrences = 0;
                for(occurrences = 0;inputEntry.contains(inputCharacter); occurrences++ ){
                    System.out.println("There is " + occurrences + " of " + inputCharacter);
                }
            }
        }
        else{
            System.out.println("Your string is too long.");
        }
    }
}
因此,如果用户输入hello,然后输入e,那么代码应该表示出现了1次e。

代码中的inpuntry.containsinputCharacter始终返回true=>无尽循环

您可以根据需要更改为indexOf

int lastIndex = 0;
int count = 0;

while(lastIndex != -1){

    lastIndex = inputEntry.indexOf(inputCharacter,lastIndex);

    if(lastIndex != -1){
        count ++;
        lastIndex += inputCharacter.length();
    }
}
您可以将代码更改为

 if (inputCharacter.length() == 1){
      int lastIndex = 0;
      int count = 0;

      while(lastIndex != -1){

        lastIndex = inputEntry.indexOf(inputCharacter,lastIndex);

        if(lastIndex != -1){
            count ++;
            lastIndex += inputCharacter.length();
        }
       }
       System.out.println("There is " + count + " of " + inputCharacter);
   }
   else{
            System.out.println("Your string is too long.");
   }

你认为循环什么时候结束?为什么?当inputEntry包含inputCharacter时,您的程序会说“保持循环”。您如何期望这不是一个无限循环?while和for循环都不会结束。我希望循环在计数发生次数后结束。因此,如果单词是Good,输入的第二个字符串是g,那么循环将读取单词Good,并看到g只出现一次,然后停止并报告一次出现。我是Java新手,所以我正在尝试查找所有可用的资源以及到目前为止我所学的内容。是的,但包含检查整个字符串的内容。阅读Javadoc!打错了什么是str?谢谢你的帮助,但我们班上还没有教过indexof。有没有办法使用我的结构?另外,如果我要添加一个打印输出语句,如System.out.PrintLn,则有+count+of+inputCharacter;它将正确打印1个字符,但如果出现多个字符,它将打印出来,因为2个字符中有1个字符。