2013-01-12 7 views
0

문자열 배열 요소에 특정 문자가 포함되어 있는지 어떻게 알 수 있습니까? 교수형 집행 인이 필요해. 사용 된 단어의 배열은 단어입니다. continueGame()은 내가 이것을 넣을 곳입니다. 나는 내 임의의 단어를 얻을 수 있다고 생각하고 편지가 그 임의의 단어에 있는지 알아보십시오. 어떻게해야합니까?문자가 배열 요소에 있음

public void continueGame(){ 
    letterSelected = JOptionPane.showInputDialog(null, "Take another guess!"); 

     if(getWords().indexOf(getWords()) == (letterSelected)){ 

     } 

     if(! getWords().contains(letterSelected)){ 
     guesses++; 
     continueGame(); 
     }else{ 
      System.out.println(letterSelected); 
     } 
    checkBodyParts(); 
    JOptionPane.showInputDialog(null, ""); 
} 
난 정말 당신이 우리에게 준 조각을 이해하지 못하는
/**Get the random word from the array*/ 
public String getWords(){ 
    String randomWord = words[randy.nextInt(words.length)]; 
    return randomWord; 
} 



/**Get the indexes of the letter of the random word indices don't start with 1*/ 
public String getSelected(){ 
    return letterSelected; 
} 

/**Finds index of the letter of randomWord*/ 
public int getIndex(){ 
    int index = getWords().indexOf(getSelected()); 
    return index; 
} 

답변

0

#.

은 그러나 당신은 지정된 문자를 포함하는 Array의 단어를 찾으려면 다음 코드를 고려할 수 :

String[] words = {"foo", "bar", "qux"}; 
for (String word : words) 
{ 
    if (word.indexOf('a') != -1) 
    { 
     System.out.println("Matched word '" + word + "' for character 'a'"); 
    } 
} 
0

난 그냥 당신의 논리를하지 않습니다. getWords()를 호출 할 때마다 새로운 임의의 단어를 반환합니다. 무작위로 생성 된 단어를 일부 변수에 할당 한 다음이 변수로 작업해야합니다.

// get your random word 
String myGeneratedRandomWord = getWords(); 

// your method 
public void continueGame(){ 
    letterSelected = JOptionPane.showInputDialog(null, "Take another guess!"); 
    if(myGeneratedRandomWord.indexOf(letterSelected) != -1) { 
     // it contains the letter 
    } 
    else { 
     // wrong guess 
    } 
} 

더 읽기에 대한 String.indexOf() 방법 : 당신의 구체적인 문제에

는이 같은해야한다.

+0

내가 "나는 바보 같아."라고 읽었을 때 웃었다. – user1972601

+0

@ user1972601 잘하고 실제로 도움이 되었습니까? StackOverflow에 대한 새로운 사용자로서 [FAQ] (http://stackoverflow.com/faq#howtoask)를 읽어 보시기 바랍니다. 감사 – Fallup

관련 문제