使用计数器在数组中搜索用户输入词



我要做的是创建一个具有设置数组的程序,程序要求用户输入一个单词,程序找到该单词并输出它被使用的次数。例如,我设置的数组(每个字母都在自己的数组编号中)是"赛盟理论",因为它在文本中有几个"the"。我希望用户输入"the"并将输出为 2(因为第一个是大写的)。或者,如果用户想输入"e",他会得到 3。

我有数组设置,但我仍在寻找一种设置程序的方法,以便在用户输入多个字母时它可以一次查看多个字母。

我假设我需要为扫描仪输入一个 .length,输入周围的计数器,然后让数组随之移动。我会更详细地研究这个问题,如果你们能帮我一把,我将不胜感激。

编辑

public class SearchableSentence {
public static void main(String[] args) throws IOException {
double count = 0;   
String[] stringer = {"T","h","e"," ","t","h","e","o","r","y"," ","o","f"," ","T","h","e","o"," ","i","s"," ","t","h","a","t"," ","t","h","e"," ","o","t","h","e","r"," ","t","h","i","n","g","s"," ","a","r","e"," ","a"," ","b","o","t","h","e","r","."};

这样的事情应该可以达到您的期望。不过我还没有测试过。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class DoWork {
    public static void main (String[] args) throws IOException{
        String[] myStringArray = {"The", "theory", "of", "thermon"};
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String searchTerm = br.readLine();
        int counter=0;
        for (String s: myStringArray) {
            if (s.contains(searchTerm)){
                counter++;
            }
        }
        System.out.println(counter);
    }
}

相关内容

  • 没有找到相关文章

最新更新