2012-03-31 37 views
-1

它會要求用戶輸入一個關鍵詞來搜索。然後,它會要求用戶反覆輸入句子。用戶可以通過鍵入「停止」,而不是一個句子停止進程(這意味着,當然,我們不能分析一個字一句話「停止」,但就是OK)。一旦用戶完成了輸入的句子,該程序應顯示以下統計:循環關鍵字計劃作業

  1. 句子的總數輸入
  2. 包含關鍵字
  3. 的平均起始位置的句子的總數包含關鍵字的句子中的關鍵字。

有人可以幫我把這個節目一起?對於#3,我們只對含有關鍵字的句子進行平均排名。

我的環部分,和#3我猜我們會使用的indexOf。 #2 inputString.contains(關鍵字)我猜?有人可以幫助我1-3並將它們放入Java程序?謝謝。

import java.util.Scanner; 

public class Lab6Loops { 

    public static void main(String[] args) { 

     String keywordString; 
     String inputString; 
     Scanner keyboard = new Scanner (System.in); 
     int numofSentences = 0; 
     int numofKeyword = 0;      
     System.out.println ("Enter a keyword. We will search each sentence for this word."); 
     keywordString = keyboard.nextLine(); 
     System.out.println ("Please enter a sentence or type 'stop' to finish"); 
     inputString = keyboard.nextLine(); 
     while(!inputString.equals ("stop")) 
     {  
      if(inputString.contains (inputString)); 
      numofSentences = numofSentences + 1; 
      if(inputString.contains (keywordString)); 
      numofKeyword = numofKeyword + 1; 
      System.out.println ("Enter a line of text or 'stop' to finish"); 
      inputString = keyboard.nextLine(); 
     } 
     System.out.println ("You entered " + numofSentences + " sentences"); 
     System.out.println ("You have " + numofKeyword + "sentences that contain the keyword"); 
    } 
} 
+0

我真的對Java。這是我第一個學期編程,所以我不太清楚如何在這裏包含我的代碼。 – user1276514 2012-03-31 18:56:05

+1

複製和粘貼是你的朋友 – 2012-03-31 19:01:32

+0

如果我複製和粘貼它,它不以代碼的形式。現在我只寫了該程序的基礎。如何格式化代碼以將其粘貼到此處? – user1276514 2012-03-31 19:10:52

回答

1

我喜歡有自記錄代碼,所以這裏是你如何能有一個很好的緊主迴路一對夫婦建議:

功能上下的語義

public void loop() { 
    // TODO: ask for the keyword and store it somewhere 

    while(true) { 
    try { 
     updateStatistics(checkOutput(getSentence())); 
    } catch (EndOfInput) { 
     printStatistics(); 
    } 
    } 
} 

面向對象

public void loop() { 
    String keyword = myPrompter.getNextSentence(); 
    myAnalyzer.setKeyword(keyword); 

    while (true) { 
    String sentence = myPrompter.getNextSentence(); 
    AnalysisResult result = myAnalyzer.analyze(sentence); 
    if (result.isEndOfInput()) { 
     myAnalyzer.printStatistics(); 
     return; 
    } 
    } 
} 

這兩種方法爲您提供了一個插入特定邏輯的簡單框架。你可以在主循環內完成所有的工作,但這會讓人感到困惑。相反,最好有一個功能完成一項任務。一個功能運行循環,另一個獲取輸入,另一計數句子#等

有時我會跟那些小功能開始,並建立從下往上的應用程序,所以我會寫一方法,它接受一個字符串並根據它是否爲字符串「stop」返回true/false。你甚至可以爲該方法編寫單元測試,這樣當你構建應用程序的其餘部分時,你就知道該方法可以達到你想要的效果。很高興有大量的模塊化組件可以在測試中使用,而不是編寫一個巨大的循環,並想知道爲什麼它沒有按照您的要求做。

0

聽起來像是你需要提示用戶的初始輸入,然後進入到一個循環,將持續到用戶按停止(或其他),在每次迭代中,你需要提示用戶輸入一個句子,如果用戶輸入數據增量一個存儲句子數的計數器,根據輸入的關鍵字對句子進行測試,並根據需要增加第二個計數器,您還需要將該單詞出現的位置推入堆棧以便稍後獲得平均值應該是堆棧的總和除以大小。你應該可以使用indexOf()來獲得這個位置。

+0

嘿湯姆,我只是包括我的代碼。我想我搞砸了關鍵字字符串,因爲它不能正確輸出用戶是否在他們的句子中輸入了關鍵字。看到有什麼不對嗎? – user1276514 2012-03-31 19:25:56

+0

你好,我想你只需要稍微擺弄一下,然後添加一個數組來保存作業的第3部分的位置,你不需要while語句中的條件,你可以在用戶輸入「停止」,也可以爲你的if/else塊使用括號:) – 2012-03-31 20:13:17

+0

你不需要保留位置的軌跡(你只被要求輸出平均位置,並且不太可能會在稍後需要數據對於任何其他的統計分析,所以只保留一個位置的運行總數,並用在@ user1276514 – 2012-03-31 20:31:22

0
package console; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 

public class Console { 

    static float averagePositionInSentence = 0; 
    static String searchTerm = ""; 
    static int noOfSentencesEntered = 0; 
    static int noOfMatches = 0; 

    public static void main(String[] args) { 
     searchTerm = writeToConsoleAndReturnInput("Add phrase to search for."); 
     writeToConsole("Now type some sentences. To exit type the word 'stop' on its own"); 
     mainInputLoop(); 
     outputResults(); 
    } 

    public static void mainInputLoop() { 

     boolean ended = false; 
     while (!ended) { 
      try { 
       String input = readLineFromConsole(); 
       if (!input.equalsIgnoreCase("stop")) { 
        maintainStatisticalData(input); 
       } else { 
        ended = true; 
       } 
      } catch (Exception e) { 
       writeToConsole("There was an error with your last input"); 
      } 
     } 
    } 

    public static void outputResults() { 
     writeToConsole("You entered " + noOfSentencesEntered + " sentences of which " + noOfMatches + " conatined the search term '" + searchTerm + "'"); 
     writeToConsole(""); 
     writeToConsole("On average the search term was found at starting position " + (averagePositionInSentence/noOfSentencesEntered) + " in the sentence"); 
    } 

    public static void maintainStatisticalData(String input) { 
     noOfSentencesEntered++; 
     if (input.contains(searchTerm)) { 
      noOfMatches++; 
      int position = input.indexOf(searchTerm)+1; 
      averagePositionInSentence += position; 
     } 
    } 

    //terminal helper methods 
    public static void writeToConsole(String message) { 
     System.out.println(message); 
    } 

    public static String writeToConsoleAndReturnInput(String message) { 
     System.out.println(message); 
     try { 
      return readLineFromConsole(); 
     } catch (IOException e) { 
      //should do something here 
      return "Exception while reading line"; 
     } 
    } 

    public static String readLineFromConsole() throws IOException { 
     InputStreamReader converter = new InputStreamReader(System.in); 
     BufferedReader in = new BufferedReader(converter); 

     return in.readLine(); 
    } 
} 
+0

我知道這是一個常見的習慣用法,但是使用縮寫'no'來表示'數字'一直在磨擦我錯誤的方法。當我讀它時,我把它看作'不匹配'。我想象你添加了'',這樣它就不會被看作'不匹配',人們會認爲這是一個布爾標誌,但是這種做法首先會使縮寫的目的失敗。爲什麼不把它稱爲'匹配',或者'numMatches'或'matchCount',如果你必須的話? – 2012-03-31 23:01:41

+0

同意 - 但使用#(不允許)或否(不是camelCase)或匹配(?),即匹配布爾...我應該只使用數字,並與它做,但我把你的觀點 – 2012-03-31 23:06:06