Java关键字平均排名

时间:2012-04-01 15:42:52

标签: java loops

  

可能重复:
  Loop Keyword Program Homework

我试图在字符串中找到用户输入的关键字的平均起始位置。我尝试过使用indexOf(),但似乎无法正确初始化它。有人可以帮助我吗?

我正在寻找的输出的一个例子是:平均起始位置是4.5

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

3 个答案:

答案 0 :(得分:0)

if循环中的2 while在最后有一个;。您应该删除它们并尝试再次运行该程序。

答案 1 :(得分:0)

扫描仪课程没有为您提供一种方法来了解句子中模式的位置。您必须使用Pattern和Matcher类来查找位置。由于这是你的作业,我不会给你代码,而是一个如何使用模式的例子......

import java.util.regex.*;
class Regex {
  public static void main(String [] args) {
     Pattern p = Pattern.compile(args[0]);
     Matcher m = p.matcher(args[1]);
     System.out.println("Pattern is " + m.pattern());
     while(m.find()) {
        System.out.println(m.start() + " " + m.group());
     }
  }

}

该程序使用第一个命令行参数(args [0])来表示 你想要使用的正则表达式,它使用第二个参数(args [1])来 表示要搜索的源数据。这是一个测试运行:

java Regex“56”“ab4 56_7ab” 产生输出:

Pattern is 56
4 56

答案 2 :(得分:0)

提示:我们通过(除了所有起始位置)除以(包含关键字的句子总数)来计算平均起始位置

以下是您的代码:

import java.util.Scanner;

/**
 * Program to calculate the average starting position of a given keyword in user input sentences.
 * We calculate average starting position by dividing (addition of all starting positions) by (total number of sentences containing keyword)
 * @author Garbage
 *
 */
public class Lab6Loops {

    public static void main(String[] args) {

        String keywordString;
        String inputString;

        Scanner keyboard = new Scanner(System.in);

        int totalSentences = 0;
        int interestingSentences = 0;
        int positionTotal = 0;

        System.out.print("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")) {
            totalSentences = totalSentences + 1; //totalSentences++

            // Check if the sentence contains our keyword
            if (inputString.contains(keywordString)){
                interestingSentences = interestingSentences + 1; // This is sentence we would like to check
                positionTotal = positionTotal + inputString.indexOf(keywordString); // Add the starting position to our total
            }
            System.out.println("Enter a line of text or 'stop' to finish");
            inputString = keyboard.nextLine();
        }
        System.out.println("You entered " + totalSentences + " sentences");
        System.out.println("You have " + interestingSentences   + "sentences that contain the keyword "+keywordString);
        System.out.println("The average starting position is "+(positionTotal / interestingSentences));
    }
}