循环关键字计划家庭作业

它会询问用户要搜索的关键字。 然后,它会要求用户一遍又一遍地输入句子。 用户可以通过键入“停止”而不是句子来停止该过程(当然,这意味着我们无法分析单词句子’停止’,但这没关系)。 用户输入完句子后,程序应显示以下统计信息:

  1. 输入的句子总数
  2. 包含关键字的句子总数
  3. 包含关键字的句子中关键字的平均起始位置。

有人可以帮我把这个程序放在一起吗? 对于#3,我们只对包含关键字的句子进行平均排名。

我有循环部分,对于#3,我猜我们会使用indexOf。 #2 inputString.contains(keyword)我在猜? 有人可以帮助我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"); } } 

我喜欢自我记录的代码,所以这里有一些关于如何有一个很好的紧密主循环的建议:

function性语义

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

这两种方法给你的是一个插入特定逻辑的简单框架。 你可以在主循环中完成所有这些操作,但这可能会让人感到困惑。 相反,最好让一个函数执行一个任务。 一个函数运行循环,另一个函数运行输入,另一个函数计算句子数等。

有时我会从那些小函数开始,并从下到上构建应用程序,所以我会编写一个接受字符串并返回true / false的方法,具体取决于它是否为字符串“stop”。 您甚至可以为该方法编写unit testing,以便在构建应用程序的其余部分时,您知道该方法可以完成您的预期。 很高兴有很多模块化组件可以沿途测试,而不是编写一个巨大的长循环,并想知道为什么它没有做你想要的。

听起来你需要提示用户输入初始输入然后输入一个循环,这个循环将一直持续到用户按下停止(或其他),在每次迭代中你需要提示用户输入一个句子,如果用户输入数据增量一个计数器存储句子数,根据输入的关键字测试句子并增加第二个计数器(如果适用),你还需要将单词出现的位置推到一个堆栈中,以便稍后获得应该是总和的平均值堆栈除以大小。 你应该能够使用indexOf()来获得这个位置。

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