![](/img/trans.png)
[英]How to use 'if' to condition and ignore a word in a particular sentence?
[英]How to search for a particular word in a sentence
我实际上是 Java 的新手,并且正在尝试做一个小项目。 因此,在我的项目中,我希望允许用户输入一个句子,并且我希望程序搜索句子中的特定单词并根据它给出输出。 我使用 NetBeans 来开发我的应用程序。
我的代码是这样的
String Sentence=jTextField1.getText();
if (Sentence.equals("Hello")
{
jTextField2.setText("Hello was found");
}
else if (Sentence.equals("Donkey")
{
jTextField2.setText("Donkey was found");
}
我知道这段代码没有意义并且不会运行,但我把它放出来是为了让人们可以大致了解我想要实现的目标。
请帮我。
像这样使用.contains
:
String Sentence = jTextField1.getText();
if (Sentence.contains("Hello")) {
jTextField2.setText("Hello was found");
} else if (Sentence.contains("Donkey")) {
jTextField2.setText("Donkey was found");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.