Java循环关键字包含 [英] Java Loop Keyword Contain

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

问题描述

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

Possible Duplicate:
Loop Keyword Program Homework

s我对此程序有问题.用户将输入一个关键字,然后输入一个句子.我需要程序输出包含关键字的句子数目以及字符串中关键字的平均起始位置.到目前为止,这就是我所拥有的.每当程序输出时,它都不会给我正确的次数输入关键字的次数.它只是给我句子的数量.有人能帮帮我吗?谢谢.

s I'm having a problem with this program. The user will enter a keyword, and then after that will enter a sentence. I need the program to output how many sentences contain the keyword, and the average starting position of the keyword in the strings. This is what I have so far. Whenever the program outputs, it doesn't give me the correct # of times the keyword is entered. It just gives me the # of sentences. Can somebody please help me? 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");
    }
}

推荐答案

也许在if语句后没有;,它会更好地工作:-)

Maybe without the ; after the if statements it will work better :-)

public static void main(String[] args)  {

    Scanner keyboard = new Scanner (System.in);

    System.out.println("Enter a keyword. We will search each sentence for this word.");
    String keywordString = keyboard.nextLine();
    System.out.println("Please enter a sentence or type 'stop' to finish");
    String inputString = keyboard.nextLine();

    int numofSentences = 0;
    int numofKeyword = 0;                       
    while (!inputString.equals("stop"))
    {       
        if (inputString.contains(inputString))
            numofSentences++;
        if (inputString.contains(keywordString))
            numofKeyword++;

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

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

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