2016-09-25 2 views
0
/** 
* 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 (위치)를 반환하는지 확인해야합니다. 누군가 제발 도와 줄 수 있니?indexOf (문자열 이름) 메소드에 문제가 있습니다. Java

+0

정확히 무엇을하고 싶습니까? –

+0

문자열 배열이 있습니까? –

+0

plz 코드 달성에 도움을 주시고 코드에서 직면 한 문제에 대해 좀더 구체적으로 말씀 해주십시오 – mhasan

답변

0

문자열 배열이 필요합니다. 모든 문자열을 반복하고 이름과 비교하기 만하면됩니다. 대소 문자가 중요하지 않으면 대문자로 된 문자열을 비교해야합니다. 여기 내 제안이 있습니다 :

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; 
} 
+0

대단히 감사합니다. 매력처럼 지금 작동 :) – SpatZan

관련 문제