2014-09-01 65 views
1

匹配所有字符的短語中的所有字符的狀態我在Java中,下面的方法:報告字母表中的

void searchPhrase(String[] phrase){ 
     searchResult = new int[phrase.length]; 
     int j=0, k=0; 
     for (int i=0; i<frase.length;i++) 
      if (!phrase[i].equals(alphabet[k])){ 
       System.out.println("\nLetter "+phrase[i]+" was not found when comparing it to "+alphabet[k]); 
       k++; 
      } 
      else{    
       System.out.println("\nLetter "+phrase[i]+" was found when comparing it to "+alphabet[k]); 
       searchResult[j] = i; 
       k=0; 
      } 
    } 

我有兩個字符串數組,短語和字母。短語是任何給定的短語,並且字母表包含從A到Z的字母表。 我需要接收短語的方法,例如:「HELLO STACK」。然後,它必須將每個字母與字母表進行比較,如下所示:

H == A?號碼
H == B?
...
H == H?是

然後,searchResult [0] = 7,因爲短語[i]中的字母H等於字母表[7]。現在,它被發現字母H,它應該繼續與字母E,等等。 不幸的是,我的代碼是這樣做的:

H == A?號碼
E == B? No.
L == C?號碼
L == D?號碼
O == E?
(空格) == F?號碼
S == G?它繼續,直到它完成「HELLO堆棧」這個詞組。如果沒有在字母表中找到,我能做些什麼來阻止它進入下一封信? 非常感謝!

+1

在for循環,我看到 「frase.length」。這是另一個變量? – patricK 2014-09-01 01:17:33

+0

爲什麼不使用'String.indexOf()'? – 2014-09-01 01:26:49

+0

@ user3651293答案貼出你接受與輸出 – 2014-09-01 01:32:40

回答

1

如果我明白你想要做什麼,代碼應該是:

void searchPhrase(String[] phrase){ 
    searchResult = new int[phrase.length]; 
    int j=0; 
    for (int i=0; i<frase.length;i++){ 
     for (int k=0; k<alphabet.length;k++){   
      if (!phrase[i].equals(alphabet[k])){ 
       System.out.println("\nLetter "+phrase[i]+" was not found when comparing it to "+alphabet[k]); 
       k++; 
      } 
      else{    
       System.out.println("\nLetter "+phrase[i]+" was found when comparing it to "+alphabet[k]); 
       searchResult[j] = i; 
       k=0; 
       break; 
      } 
     } 
    } 
} 

我沒有測試代碼,但它應該工作。

+0

謝謝!它工作得很好,我只在內部刪除了k ++,因爲字母表是A,C,F,H ......謝謝! – user3651293 2014-09-01 01:39:13

1

爲了得到這個邏輯,use another (nested) loop

外層循環遍歷短語中的字符(如完成),內層循環迭代字母表中的字符。當內循環(在字母表上循環)因匹配當前短語字符而終止時,「停止進入下一個字母」被完成。然後外層循環的下一次迭代繼續,等等,直到迭代完成。

使用break(示例代碼也顯示使用嵌套循環)在內循環內使用if時會很有幫助。另外,根據賦值的不同,當短語中的字符不在字母表中時,可能需要處理該案例。使用常規循環完成任務後,請參閱ehanced for loops,該代碼可以清理代碼,因爲不需要索引。

+0

謝謝!我這樣做,它的工作。感謝您的演示。 – user3651293 2014-09-01 01:38:16

+0

@ user3651293酷!玩得開心學習。 – user2864740 2014-09-01 02:22:49

1

如果你不想繼續,如果phrase[n] != alphabet[k]那麼這段話總是需要「ABC ......」因爲如果你使用HELLO STACK的第一個字母是H這是!= alphabet[0](A)

,你可以在這裏使用一個嵌套循環

是你的那句數組?你有幾句話嗎?如果沒有這將是功能

我寧願讓字母排列爲char[] alphabet

void searchPhrase(String phrase){ 

//string length() 
outer: 
for(int x = 0 ; x < phrase.length() ; x++){ 
    //char length 
    inner: 
    for(int y = 0 ; y < alphabet.length ; y++){ 

     if(phrase.charAt(x) == alphabet[y]){ 
      //alphabet found 
     }else{ 
      //not found 
     break outer; 
     } 
    } 
    } 
} 
1

這是你想要的嗎?

void searchPhrase(String[] phrase){ 
      searchResult = new int[phrase.length]; 
      int j=0, m=-1, n=-1; 
      for (int i=0; i<phrase.length;i++) 
      { 
       for (int k=0; i<26;i++) 
       { 
       if (phrase[i].equals(alphabet[k])) 
       { 
       searchPhrase[i] = k;  
       System.out.println("\nLetter "+phrase[i]+" was found when comparing it to"+alphabet[k]); 
       } 
       } 
      } 
1

工作示例,同時使用字符串數組來匹配

public static void searchPhrase(String[] phrase) { 
     int[] searchResult = new int[phrase.length]; 
     String[] alphabet = "abcdefghijklmnopqrstuvwxyz".split(""); 
     int j = 0; 
     for (int i = 0 ; i < phrase.length ; i++) 
      for (int k = 0 ; k < alphabet.length ; k++) { 
       if (!phrase[i].toLowerCase().equals(alphabet[k])) { 
        System.out.println("\nLetter " + phrase[i] + " was not found when comparing it to " + alphabet[k]); 
       } else { 
        System.out.println("\nLetter " + phrase[i] + " was found when comparing it to " + alphabet[k]); 
        searchResult[j] = i; 
        break; 
       } 
      } 
    } 

輸出

Letter H was not found when comparing it to 

Letter H was not found when comparing it to a 

Letter H was not found when comparing it to b 

Letter H was not found when comparing it to c 

Letter H was not found when comparing it to d 

Letter H was not found when comparing it to e 

Letter H was not found when comparing it to f 

Letter H was not found when comparing it to g 

Letter H ** was found when comparing it to ** h 

Letter E was not found when comparing it to 

Letter E was not found when comparing it to a 

Letter E was not found when comparing it to b 

Letter E was not found when comparing it to c 

Letter E was not found when comparing it to d 

Letter E ** was found when comparing it to ** e 

Letter L was not found when comparing it to 

Letter L was not found when comparing it to a 

Letter L was not found when comparing it to b 

Letter L was not found when comparing it to c 

Letter L was not found when comparing it to d 

Letter L was not found when comparing it to e 

Letter L was not found when comparing it to f 

Letter L was not found when comparing it to g 

Letter L was not found when comparing it to h 

Letter L was not found when comparing it to i 

Letter L was not found when comparing it to j 

Letter L was not found when comparing it to k 

Letter L ** was found when comparing it to ** l 

Letter L was not found when comparing it to 

Letter L was not found when comparing it to a 

Letter L was not found when comparing it to b 

Letter L was not found when comparing it to c 

Letter L was not found when comparing it to d 

Letter L was not found when comparing it to e 

Letter L was not found when comparing it to f 

Letter L was not found when comparing it to g 

Letter L was not found when comparing it to h 

Letter L was not found when comparing it to i 

Letter L was not found when comparing it to j 

Letter L was not found when comparing it to k 

Letter L ** was found when comparing it to ** l 

Letter O was not found when comparing it to 

Letter O was not found when comparing it to a 

Letter O was not found when comparing it to b 

Letter O was not found when comparing it to c 

Letter O was not found when comparing it to d 

Letter O was not found when comparing it to e 

Letter O was not found when comparing it to f 

Letter O was not found when comparing it to g 

Letter O was not found when comparing it to h 

Letter O was not found when comparing it to i 

Letter O was not found when comparing it to j 

Letter O was not found when comparing it to k 

Letter O was not found when comparing it to l 

Letter O was not found when comparing it to m 

Letter O was not found when comparing it to n 

Letter O ** was found when comparing it to ** o 

Letter S was not found when comparing it to 

Letter S was not found when comparing it to a 

Letter S was not found when comparing it to b 

Letter S was not found when comparing it to c 

Letter S was not found when comparing it to d 

Letter S was not found when comparing it to e 

Letter S was not found when comparing it to f 

Letter S was not found when comparing it to g 

Letter S was not found when comparing it to h 

Letter S was not found when comparing it to i 

Letter S was not found when comparing it to j 

Letter S was not found when comparing it to k 

Letter S was not found when comparing it to l 

Letter S was not found when comparing it to m 

Letter S was not found when comparing it to n 

Letter S was not found when comparing it to o 

Letter S was not found when comparing it to p 

Letter S was not found when comparing it to q 

Letter S was not found when comparing it to r 

Letter S ** was found when comparing it to ** s 

Letter T was not found when comparing it to 

Letter T was not found when comparing it to a 

Letter T was not found when comparing it to b 

Letter T was not found when comparing it to c 

Letter T was not found when comparing it to d 

Letter T was not found when comparing it to e 

Letter T was not found when comparing it to f 

Letter T was not found when comparing it to g 

Letter T was not found when comparing it to h 

Letter T was not found when comparing it to i 

Letter T was not found when comparing it to j 

Letter T was not found when comparing it to k 

Letter T was not found when comparing it to l 

Letter T was not found when comparing it to m 

Letter T was not found when comparing it to n 

Letter T was not found when comparing it to o 

Letter T was not found when comparing it to p 

Letter T was not found when comparing it to q 

Letter T was not found when comparing it to r 

Letter T was not found when comparing it to s 

Letter T ** was found when comparing it to ** t 

Letter A was not found when comparing it to 

Letter A ** was found when comparing it to ** a 

Letter C was not found when comparing it to 

Letter C was not found when comparing it to a 

Letter C was not found when comparing it to b 

Letter C ** was found when comparing it to ** c 

Letter K was not found when comparing it to 

Letter K was not found when comparing it to a 

Letter K was not found when comparing it to b 

Letter K was not found when comparing it to c 

Letter K was not found when comparing it to d 

Letter K was not found when comparing it to e 

Letter K was not found when comparing it to f 

Letter K was not found when comparing it to g 

Letter K was not found when comparing it to h 

Letter K was not found when comparing it to i 

Letter K was not found when comparing it to j 

Letter K ** was found when comparing it to ** k 
+0

@ user2864740 done,去掉輸出 – 2014-09-01 01:33:32

+0

@ user2864740 dnt知道,hot post輸出 – 2014-09-01 01:36:26

+0

@ user2864740現在完成了,謝謝 – 2014-09-01 01:37:29