繁体   English   中英

使用indexOf(string name)方法遇到麻烦。 爪哇

[英]Having trouble with indexOf(string name) method. Java

/**
* return the index of the Predator with the given name.  Case does not  matter.

* No External Classes Permitted to Be Used in This Method Apart from 
* String.Equals and String.ToUpperCase
* My Solution Length in Lines (note yours can be longer or shorter): 4
*
* @param name the name of the Predator to be found
* @return the position of the searched for Predator.  -1 if none found
*/

public int indexOf(String name) {

}

我知道我必须做的事情,但是我不确定如何解决它。 所以我知道我必须遍历数组,看看名称是否等于该元素中的名称,如果它确实返回了INDEX(位置)。 有人可以帮忙吗?

我猜你需要有字符串数组。 您需要做的就是遍历每个字符串,并将其与名称进行比较。 如果大小写无关紧要,则必须比较大写的字符串。 这是我的主张:

public int indexOf(String[] array, String name) { 
    for (int i=0; i<array.length; i++)
        if (array[i].toUpperCase().equals(name.toUpperCase()))
            return i;       
    return -1;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM