2013-05-05 2 views
1

교수형 집행 프로그램을 만들려고합니다 ... 다른 문제와 함께 찾을 수 없거나 찾을 수없는 방법을 만들어서 찾을 수없는 경우 발견 된 모든 문자를 대체하십시오. findAndReplace() 메서드를 사용하여이 문제를 해결하려고 노력하고 있습니다 ... 작동하지 않았습니다. 사람은 새 것으로 StringBuilder 교체 나에게 다음StringBuilder 개체에서 문자가 두 번 이상 발견되는 경우

public class Hangman { 


static Scanner sc = new Scanner(System.in); 
     static final int MAXTRYS = 10; 
     static int numError = 0; 
     static boolean FINSHED = false; 
     String s; 
     String input; 
     String tW; 
     int pos; 


     public static void main(String[] args) { 
      System.out.println("Welcome to the Hangman App" + "\n"); 
      String s = answerKey(); 
      StringBuilder AnswerKey = dashReplace(s); 




      while(!FINSHED && numError < MAXTRYS){ 
      //get user input and cast to char 
      System.out.println("Enter an Letter: "); 
      String input = sc.nextLine(); 
      char ch = input.charAt(0); 


      int pos = findAndReplace(s, AnswerKey, input, ch); 
      int index = AnswerKey.indexOf("-"); 
      if(pos == -1){ 
       numError++; 
       continue; 
      }else if(index == -1){ 
       FINSHED = true; 
       System.out.println("you won!"); 
      } 
      } 

     } 


     public static int findAndReplace(String s, StringBuilder AnswerKey, 
       String input, char ch) { 
      //find position of user input and replace 
      int pos = s.indexOf(input); 
      AnswerKey.setCharAt(pos, ch); 
      // while(pos > 0) { 
      // AnswerKey.setCharAt(pos, (char) (ch+1)); 
      //} 
      System.out.println(AnswerKey); 
      return pos; 
     } 


     public static StringBuilder dashReplace(String s) { 
      //replace non-white space char with dashes and creates StringBuilder Object 
      String tW = s.replaceAll("\\S", "-"); 
      System.out.print(tW + "\n"); 
      StringBuilder AnswerKey = new StringBuilder(tW); 
      return AnswerKey; 
     } 


     public static String answerKey() { 
      //get random array element 
      String array[] = new String[10]; 
      array[0] = "Hamlet"; 
      array[1] = "Mysts of Avalon"; 
      array[2] = "The Iliad"; 
      array[3] = "Tales from Edger Allan Poe"; 
      array[4] = "The Children of Hurin"; 
      array[5] = "The Red Badge of Courage"; 
      array[6] = "Of Mice and Men"; 
      array[7] = "Utopia"; 
      array[8] = "Chariots of the Gods"; 
      array[9] = "A Brief History of Time"; 

      ArrayList<String> list = new ArrayList<String>(Arrays.asList(array)); 
      Collections.shuffle(list); 
      String s = list.get(0); 
      //for testing 
      System.out.println(s); 
      return s; 
     } 

} 

답변

2

일반적인 "찾기 및 바꾸기"루프는 다음과 같습니다.

// Search starts at pos+1, so we set pos to -1 to start search at zero 
int pos = -1; 
// We'll break out of the loop when there are no further matches 
while (true) { 
    // Look for the desired character starting one character past the pos 
    pos = s.indexOf(input, pos+1); 
    // If the character is not found, end the loop 
    if (pos < 0) break; 
    // Otherwise, replace the character 
    s.setCharAt(pos, ch); 
} 

위의 코드에서 주석을 참조하십시오. 계속하고있어.

+0

정말 고맙습니다. 한 번에 두 가지 문제를 해결하고 매우 감사하게 생각합니다. – Sage1216

0
가장 쉬운 해결책은 replace() 방법을 사용하여 StringBuilder String로 변환하는 것 같다

하고 도움을 줄 수하십시오

answerKey = new StringBuilder(answerKey.toString().replace(charToReplace, newChar));