报告短语中所有字符与字母表中所有字符的匹配状态

时间:2014-09-01 01:03:07

标签: java arrays string

我在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;
            }
    }

我有两个String数组,短语和字母。短语是任何给定的短语,字母表包含从A到Z的字母表。 我需要方法来接收一个短语,让我们说:“HELLO STACK”。然后,它必须将每个字母与字母表进行比较,如下所示:

   H == A?第
   H == B?第
......    H == H?是

     然后,searchResult [0] = 7,因为短语[i]中的字母H等于字母[7]。现在它找到了字母H,它应该继续用字母E,依此类推。 不幸的是,我的代码是这样做的:

   H == A?第
   E == B?第
   L == C?第
   L == D?第
   O == E?第
   (空格) == F?第
   S == G?第
它一直持续到完成短语“HELLO STACK”。如果在字母表中找不到它,我该怎么办才能阻止它进入下一个字母? 非常感谢!

5 个答案:

答案 0 :(得分:1)

要获得这种逻辑,use another (nested) loop

外部循环遍历短语中的字符(完成后),内部循环迭代字母表中的字符。 &#34;阻止它进入下一个字母&#34;当内循环(在字母表上循环)终止,因为它匹配当前短语字符时完成。然后继续外循环的下一次迭代,依此类推,直到短语完成迭代。

在内部循环中使用if时,使用break(示例代码也显示使用嵌套循环)会很有帮助。此外,根据分配,可能需要处理短语中的字符不在字母表中的情况。使用常规循环完成任务后,请参阅ehanced for loops,它可以清理代码,因为不能摆弄任何索引。

答案 1 :(得分:1)

如果您不希望继续phrase[n] != alphabet[k]那么该短语总是需要&#39; abc .....&#39;因为如果您使用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;
         }
     }
  }
 }

答案 2 :(得分: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;
            }
        }
    }
}

我还没有对代码进行测试,但它应该可以运行。

答案 3 :(得分: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]);
                }
               }
             }

答案 4 :(得分:1)

工作示例,使用两个String数组匹配

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