Java关键字平均排名 [英] Java Keyword Average Position

查看:87
本文介绍了Java关键字平均排名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
循环关键字程序作业

Possible Duplicate:
Loop Keyword Program Homework

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

I'm trying to find the average starting position of the user inputted keyword in a string. I've tried using indexOf() but I can't seem to initialize it correctly. Can somebody help me here?

我要寻找的输出示例为:平均起始位置为4.5

An example of the output I'm looking for would be: The average starting position is 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");
    }
}

推荐答案

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

Hint: We calculate average starting position by dividing (addition of all starting positions) by (total number of sentences containing keyword)

这是给您的代码:

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

这篇关于Java关键字平均排名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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