2012-10-24 192 views
2

我正在爲我的作業寫一個hang子手程序,並遇到一個小問題。 在我的程序中,爲了分開單詞,我使用'|'符號。 例如這是這個詞來猜測:如何在單詞之間添加分隔符?

U N I T E D | S T A T E S | O F | A M E R I C A 

我有顯示沒有一個StringBuffer變量。破折號 這裏,

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

我試着用下面的代碼插入分隔符「|」

if(guessword.indexOf('|') == -1) 
    System.out.print(" "); 
else 
    guessit.setCharAt(guessword.indexOf('|'),'|'); 

我知道它錯了,它只插入1個分隔符。 你能幫我把分離器插在正確的位置嗎? 也可以嘗試刪除System.out.print(「」);因爲它不必要地留下空間。 我可以使用break嗎?而不是 對於上述例如我得到的顯示器作爲

_ _ _ _ _ _ | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

其實我是想

_ _ _ _ _ _ | _ _ _ _ _ _ | _ _ | _ _ _ _ _ _ _ 

我的主要方法如下

public static void main()throws IOException 
{ 
    Hangman obj = new Hangman(); 
    InputStreamReader isr = new InputStreamReader(System.in); 
    BufferedReader br = new BufferedReader(isr); 
    PrintWriter p = new PrintWriter(System.out,true); 

    p.println("Lets play HANGMAN"); 
    p.println(); 
    p.println("Enter your choice according to the following options.\n 
       NOTE: Words are related to the topics given below.\n 
       1. Sports\n2. Movies\n3. Computer\n4. Food\n5. Countries"); 
    p.println(); 
    int choice = Integer.parseInt(br.readLine()); 
    obj.clearScreen(); 
    String bothwordandclue[] = new String[2]; 

    if(choice == 1) 
     bothwordandclue = obj.Sports(); 
    else if(choice == 2) 
     bothwordandclue = obj.Movies(); 
    else if(choice == 3) 
     bothwordandclue = obj.Computers(); 
    else if(choice == 4) 
     bothwordandclue = obj.Food(); 
    else if(choice == 5) 
     bothwordandclue = obj.Countries(); 
    else 
     p.println("Wrong choice"); 

    int counter = 6; 
    String guessword = bothwordandclue[0]; 
    String wordclue = bothwordandclue[1]; 

    int lengthofword = (int)(Math.round(((double)guessword.length()/2))); 
    int checkguess = 0; 
    String a; 
    StringBuffer guessit = new StringBuffer(); 

    for (int i = 0;i < lengthofword; i++) 
     guessit = guessit.append("_ "); 

    guessit.delete((2 * lengthofword)-1,(2 * lengthofword)); 

    if(guessword.indexOf('|') == -1) 
     System.out.print(" "); 
    else 
     guessit.setCharAt(guessword.indexOf('|'),'|'); 

    p.println(guessit); 
    do 
    { 
     p.println(); 
     p.println("Enter your guess letter in capitals"); 
     String guessletter = br.readLine(); 
     obj.clearScreen(); 

     for(int i = 0;i<lengthofword;i++) 
     { 
      if(guessletter.charAt(0) == guessword.charAt(2*i)) 
      { 
       guessit.setCharAt(2*i,guessletter.charAt(0)); 
       checkguess=1; 
      }     
     } 
     if(checkguess == 1) 
     { 
      p.println("Correct Guess"); 
      p.println(guessit); 
     } 
     else 
     { 
      counter--; 
      p.println("Wrong guess. You have " + counter + 
            " incorrect guesses left"); 
      p.println(guessit); 
     } 
     checkguess = 0; 
     a = guessit.toString(); 
     if(a.equals(guessword)) 
     { 
      p.println("You guessed the word!!!!!"); 
      counter=0; 
     }   
    }while(counter>0); 
} 

回答

1

只能與indexOf做到這一點。只需要使用該方法的另一個版本,即可從哪裏開始搜索。見你需要的String#indexOf(String str, int fromIndex)

int index = guessword.indexOf("|"); 
while(index >= 0) { 
    guessIt.setCharAt(index, '|') 

    // Start searching for next "|" after this index 
    index = guessword.indexOf("|", index+1)); 
} 
+0

Upvote因爲它比我的更有效地解決了問題,但對於初學者來說並不明顯。也許包含http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String的鏈接。html#indexOf(java.lang.String,int) –

+0

@Phhite。當然:) –

+0

非常感謝。這非常有效地解決了問題。 – crvineeth99

0
+0

效率更高,但我不喜歡告訴初學者學習正則表達式的想法。 –

+1

這裏不需要它們。你爲什麼認爲正則表達式會起作用?我們將根據其他字符串替換字符串中的字符 –

+0

@Phhite如果是爲了分配,我想他有一天會學習正則表達式,那麼爲什麼不今天呢? –

1

的問題是,setCharAtindexOf只適用於每一個指標,這就是爲什麼它只是更換第一|。您將不得不多次遍歷guessword。您可以使用一些Java的其他內置字符串函數或使用一個簡單的for循環,即

for(int i = 0; i < guessword.length(); i++){ 
    if(guessword.charAt(i) == '|') 
     guessit.setCharAt(i, '|'); 
} 
+0

謝謝你的發帖。在發佈這個問題之前,我已經嘗試過了,它甚至沒有在單詞之間加上一個分隔符:D – crvineeth99

相關問題