我如何使用 java 中的 indexOf 检查数组中的数据
how do i check the data in an array with indexOf in java
我是 java 的新手,我正在尝试使用 indexOf 检查一个人的名字是否以数组中的字母结尾,并输出一个与其押韵的单词。我哪里错了?例如Dean 以 "ean" 结尾,因此它与绿色押韵。谢谢大家
String [] first = new String [3];
first [0] = "eem";
first [1] = "een";
first [2] = "ean";
for (int i = 0; i < first.length; i++) {
if (first[i].indexOf(first.length) != -1){
System.out.println("That rhymes with green");
}
}
要检查天气输入是否包含给定元素的任何数组,您应该收到 input
然后遍历您的数组以查看。例如
String personname = "Dean";
String [] first = new String [3];
first [0] = "eem";
first [1] = "een";
first [2] = "ean";
for (int i = 0; i < personname.length; i++) {
if (input.indexOf(first[i]) != -1){ // check my input matched
System.out.println("That rhymes with green");
}
}
我已经在编译器上测试并运行它。这工作正常。如有任何问题,请发表评论。谢谢
import java.util.*;
public class HelloWorld
{
public static void main(String []args)
{
String [] first = new String [3];
first [0] = "eem";
first [1] = "een";
first [2] = "ean";
/* I am trying to get the input from user here */
String s;
Scanner in = new Scanner(System.in);
System.out.println("Enter the string:");
s = in.nextLine();
/* Now, String.indexOf(substring) will check the condition if the match happens it will print the output, if it doesn't it returns -1 */
for (int i = 0; i <s.length(); i++)
{
if (s.indexOf(first[i]) != -1)
{
System.out.println("That rhymes with green");
}
}
}
}
你应该使用 endsWith instead of indexOf。 indexOf
将 return 传递的字符串与当前字符串完全匹配的索引,顾名思义,endsWith
将检查当前字符串是否以传递的字符串结尾。
看看下面的代码:
String personName = "Dean";
String[] suffix = {"eem", "een", "ean"};
String[] names = {"greem", "green", "grean"};
for(int i = 0; i < suffix.length; i++) {
if (personName.endsWith(suffix[i])){
System.out.println("That rhymes with " + names[i]);
}
}
此外,理想情况下,您希望保留 suffix -> name
的地图以便于维护,但对于 simplicity/exploring 这应该没问题。
我是 java 的新手,我正在尝试使用 indexOf 检查一个人的名字是否以数组中的字母结尾,并输出一个与其押韵的单词。我哪里错了?例如Dean 以 "ean" 结尾,因此它与绿色押韵。谢谢大家
String [] first = new String [3];
first [0] = "eem";
first [1] = "een";
first [2] = "ean";
for (int i = 0; i < first.length; i++) {
if (first[i].indexOf(first.length) != -1){
System.out.println("That rhymes with green");
}
}
要检查天气输入是否包含给定元素的任何数组,您应该收到 input
然后遍历您的数组以查看。例如
String personname = "Dean";
String [] first = new String [3];
first [0] = "eem";
first [1] = "een";
first [2] = "ean";
for (int i = 0; i < personname.length; i++) {
if (input.indexOf(first[i]) != -1){ // check my input matched
System.out.println("That rhymes with green");
}
}
我已经在编译器上测试并运行它。这工作正常。如有任何问题,请发表评论。谢谢
import java.util.*;
public class HelloWorld
{
public static void main(String []args)
{
String [] first = new String [3];
first [0] = "eem";
first [1] = "een";
first [2] = "ean";
/* I am trying to get the input from user here */
String s;
Scanner in = new Scanner(System.in);
System.out.println("Enter the string:");
s = in.nextLine();
/* Now, String.indexOf(substring) will check the condition if the match happens it will print the output, if it doesn't it returns -1 */
for (int i = 0; i <s.length(); i++)
{
if (s.indexOf(first[i]) != -1)
{
System.out.println("That rhymes with green");
}
}
}
}
你应该使用 endsWith instead of indexOf。 indexOf
将 return 传递的字符串与当前字符串完全匹配的索引,顾名思义,endsWith
将检查当前字符串是否以传递的字符串结尾。
看看下面的代码:
String personName = "Dean";
String[] suffix = {"eem", "een", "ean"};
String[] names = {"greem", "green", "grean"};
for(int i = 0; i < suffix.length; i++) {
if (personName.endsWith(suffix[i])){
System.out.println("That rhymes with " + names[i]);
}
}
此外,理想情况下,您希望保留 suffix -> name
的地图以便于维护,但对于 simplicity/exploring 这应该没问题。