使用计数器在数组中搜索用户输入的单词
Search for a user input word in an array by using a counter
我想做的是创建一个有一组数组的程序,程序要求用户输入一个词,程序找到这个词并输出它被使用了多少次。因此,例如我设置的数组(每个字母在其自己的数组编号中)是 "The theory of thermon" 因为它在文本中有几个 '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);
}
}
我想做的是创建一个有一组数组的程序,程序要求用户输入一个词,程序找到这个词并输出它被使用了多少次。因此,例如我设置的数组(每个字母在其自己的数组编号中)是 "The theory of thermon" 因为它在文本中有几个 '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);
}
}