它會要求用戶輸入一個關鍵詞來搜索。然後,它會要求用戶反覆輸入句子。用戶可以通過鍵入「停止」,而不是一個句子停止進程(這意味着,當然,我們不能分析一個字一句話「停止」,但就是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