2012-03-31 94 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