循环关键字程序作业 [英] Loop Keyword Program Homework

查看:84
本文介绍了循环关键字程序作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它将询问用户要搜索的关键字.然后,它将要求用户反复输入句子.用户可以通过键入停止"而不是句子来停止该过程(这当然意味着我们无法分析一个单词的句子停止",但是可以).用户输入完句子后,程序应显示以下统计信息:

It will ask the user for a keyword to search for. Then, it will ask the user to enter sentences over and over. The user can stop the process by typing "stop" instead of a sentence (which means, of course, that we can’t analyze the one word sentence ‘stop’, but that is OK). Once the user has finished entering the sentences, the program should display the following statistics:

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

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

Can somebody help me put this program together? For #3 we only do average position of the sentences that contain the keyword.

我有循环部分,对于#3,我猜我们将使用indexOf. #2 inputString.contains(keyword)我在猜吗?有人可以帮我1-3并将它们放入Java程序吗?谢谢.

I have the loop part, and for #3 I'm guessing we would use indexOf. #2 inputString.contains(keyword) I'm guessing? Can somebody help me with 1-3 and putting them into a Java program? Thanks.

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

推荐答案

我喜欢使用自记录代码,因此,这里有一些建议,可以使您拥有一个很好的紧紧的主循环:

I like having self-documenting code, so here are a couple suggestions for how you can have a nice tight main loop:

功能性语义

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

这两种方法都为您提供了一个插入特定逻辑的简单框架.您可以在主循环中完成所有操作,但这可能会造成混淆.相反,最好让一个功能完成一项任务.一个函数运行循环,另一个函数获取输入,另一个函数计算句子的数量,等等.

What both of these approaches gives you is a simple framework to plug in the specific logic. You could do all of it inside the main loop, but that can get confusing. Instead, it's preferable to have one function doing one task. One function runs the loop, another gets the input, another counts the # of sentences, etc.

有时候,我将从这些小功能开始,并从下至上构建应用程序,因此我会编写一个方法,该方法接受字符串,并根据字符串是否为"stop"返回true/false.您甚至可以为该方法编写单元测试,以便在构建应用程序的其余部分时,您知道该方法可以达到预期的目的.拥有很多可以在此过程中进行测试的模块化组件真是太好了,而不是编写一个巨大的长循环并想知道为什么它没有按照您的要求做.

Sometimes I'll start with those little functions, and build the app from the bottom-up, so I'd write a method that takes a string and returns true/false depending on if it's the string "stop". You can even write unit tests for that method, so that when you're building the rest of the app, you know that method does what you intended it to. It's nice to have lots of modular components that you can test along the way, rather than writing a huge long loop and wondering why it's not doing what you want.

这篇关于循环关键字程序作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆