2015-04-29 152 views
0

我想要做的是創建一個具有set數組的程序,程序要求用戶輸入一個單詞,程序找到該單詞並輸出很多次它被使用。例如,我設置的數組(每個字母都有自己的數組編號)是「熱門理論」,因爲它在文本中有幾個「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","."}; 
+0

您是否製作了任何代碼? –

+1

很高興看到一些代碼。我們在這裏幫助你,而不是做你的功課:)。 –

+0

分享你的代碼片段。以便我們能夠提供幫助 –

回答

3

像這樣的東西應該做你的期望。我還沒有測試過它。

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); 
    } 
}