2014-09-01 73 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