gpt4 book ai didi

java - java中使用命令行进行编程

转载 作者:行者123 更新时间:2023-12-01 07:24:53 25 4
gpt4 key购买 nike

我需要为学校制作一个输入 2 个字符串的程序 - 其中一个包含一个句子,另一个包含一个单词。程序应该搜索该单词在句子中出现了多少次。我做了这样的程序(下面的代码)并且它可以工作,但是老师拒绝接受这种方式。她希望我让它与“命令行”一起工作,因为我是初学者,所以我不明白这是如何工作的。希望获得有关代码的一些帮助...

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

最佳答案

莱埃罗说的是正确的!

String text = args[0];
String word = args[1];

执行程序时:

java Search "this is the sentence containing words" "words"

关于java - java中使用命令行进行编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26719480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com