我需要爲進入2個字符串的學校編寫程序 - 其中一個包含一個句子,另一個包含一個單詞。該程序應該搜索該單詞在句子中存在多少次。我做了這樣的程序(下面的代碼),它的工作,但老師拒絕接受這種方式。她希望我能使它與「命令行」一起工作,並且因爲我是一個先行者,我不明白這將如何工作。希望對代碼有一些幫助...在java中使用命令行編程
import java.util.Scanner;
class Search{
public static void main(String args[])
{
String text;
String word;
int found=0;
Scanner in = new Scanner(System.in);
System.out.println("Sentence: ");
text= in.nextLine();
System.out.println("Word: ");
word=in.nextLine();
for (int i = -1; (i = text.indexOf(word, i + 1)) != -1;)
{
found++;
}
System.out.println("The word was found "+found+" times!");
}
}
你問過你的老師澄清?他們說了什麼? – 2014-11-03 17:23:30
String [] args是你將要使用的變量 – LeatherFace 2014-11-03 17:23:32
用引號輸入句子(「像這樣的東西。」),然後它是一個命令行參數。 – 2014-11-03 17:26:16