了,我就是有这个程序有问题。用户将输入一个关键字,然后再输入一个句子。我需要程序输出多少句子包含关键字,以及关键字在字符串中的平均起始位置。这是我迄今为止所拥有的。只要程序输出,它不会给我输入关键字的正确次数。它给我的句子#。有人能帮帮我吗?谢谢。
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");
}
}
你从读者期待什么好?整个解决方案都为您烹制或修复了一个您无法找到的错误?请确切说明你的期望。 – Vincent 2012-04-01 06:22:13
我希望有人能够向我解释我在做什么错误,导致输出不能正确保留包含用户输入关键字的句子数。我对Java真的很陌生,所以我知道这是初学者的东西。 – user1276514 2012-04-01 06:24:46
不要在你的if语句头后放置分号:'if(inputString.contains(inputString)); < - no';'' – 2012-04-01 06:30:15