gpt4 book ai didi

java - 我的方法可以查找字符串中的每个单词吗

转载 作者:行者123 更新时间:2023-12-02 13:31:21 24 4
gpt4 key购买 nike

正如标题所示,我正在尝试创建一种可以单独处理字符串中每个单词的方法。我已经把代码写下来了,但我不确定它是否正确。因此,我进行了几次测试,看看它是否正确打印。经过多次尝试,完全没有打印出来。我需要帮助。谁能发现我的代码有什么问题吗?

public static String build( String str4, one test){
Scanner find = new Scanner( System.in);
String phrase = " ";
while ( find.hasNext()){
String word = find.next();
word = test.change(word);
phrase += word + " ";

}
return phrase;
}

方法更改只是将单词更改为“pig Latin”(我的预期目标)。

以下是我的主要方法中的简单代码:

 String str4 = "I am fluent in pig latin";
System.out.println (test.build(str4, test));

我打算让这段代码打印出这个:

Iyay amyay uentflay inyay igPay atinLay

最佳答案

您尝试使用 Scanner 实例在函数内部获取一些输入,并将用户输入作为其构造参数。

为了打印要返回的内容,请添加以下行:

System.out.println (phrase);

在返回声明之前。

但我猜测,您错误地使用了用户输入。试试这个:

public static String build( String str4, one test){
Scanner find = new Scanner(str4);
String phrase = " ";
while ( find.hasNext()){
String word = find.next();
word = test.change(word);
phrase += word + " ";
}
//Print your phrase here if you want.
System.out.println(phrase);
return phrase;

}

关于java - 我的方法可以查找字符串中的每个单词吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28823024/

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