它会要求用户输入一个关键词来搜索。然后,它会要求用户反复输入句子。用户可以通过键入“停止”,而不是一个句子停止进程(这意味着,当然,我们不能分析一个字一句话“停止”,但就是OK)。一旦用户完成了输入的句子,该程序应显示以下统计:循环关键字计划作业
- 句子的总数输入
- 包含关键字
- 的平均起始位置的句子的总数包含关键字的句子中的关键字。
有人可以帮我把这个节目一起?对于#3,我们只对含有关键字的句子进行平均排名。
我的环部分,和#3我猜我们会使用的indexOf。 #2 inputString.contains(关键字)我猜?有人可以帮助我1-3并将它们放入Java程序?谢谢。
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");
}
}
我真的对Java。这是我第一个学期编程,所以我不太清楚如何在这里包含我的代码。 – user1276514 2012-03-31 18:56:05
复制和粘贴是你的朋友 – 2012-03-31 19:01:32
如果我复制和粘贴它,它不以代码的形式。现在我只写了该程序的基础。如何格式化代码以将其粘贴到此处? – user1276514 2012-03-31 19:10:52