java - 样式(递归和越界异常已得到解答,这是新的)

标签 java recursion anagram

我正在尝试编写一个程序,该程序接受两个单词或短语,并通过查看它们的 unicode 值是否匹配来测试它们是否是字谜词。 “搜索”方法仅在它们长度相同时才运行。我遇到了问题,但现在已解决。

这是修改后的版本:

我想知道您对代码布局的风格有何看法。清楚吗?我应该采取不同的做法吗?或者你觉得它很容易阅读? 您对我如何使其他人更清楚有什么建议吗?

如果我添加注释,它们应该很短还是应该在多行注释中解释该部分如何工作?

我想让它看起来尽可能简单,但我对此几乎没有得到真正的建议。所以如果有人有任何想法......

import java.util.Scanner;

public class AnagramCount {

   public static void main(String[] args) {
   System.out.println("Please enter two words, one per line, to test if it is an anagram");
   Scanner userInput = new Scanner(System.in);
   String word1 = userInput.nextLine();
   String word2 = userInput.nextLine();
   int count = 0;
   int[] char_code = new int[word1.length()];
   int[] char_code2 = new int[word2.length()];
   char[] temp = word2.toCharArray();
   boolean match = true;

   if (word1.length() == word2.length()){
      search(word1, word2, count, char_code, char_code2, match, temp);
      if (match == true){
         if (char_code[word1.length()-1] == 0){
            match = false;
         }
      else {
      // if match remains true after this final check, information about it will print
         System.out.print("word1 unicode values: ");
         for(int i = 0; i < word1.length(); i++){
            System.out.print(char_code[i] + " ");
         }
         System.out.println();
         System.out.print("word2 unicode values: ");
         for(int i = 0; i < word1.length(); i++){
            System.out.print(char_code2[i] + " ");
         }
      }
   }
}
   else {
      match = false;
   }
   System.out.println("\n" + "Anagram? t/f?: " + match);
}

public static void search(String word1, String word2, int count, int[] char_code, int[]   char_code2, boolean match, char[] temp)
{
   StringBuilder word1check = new StringBuilder(word1);
   StringBuilder word2check = new StringBuilder(word2);
   int word1_unicode = 0;
   int word2_unicode = 0;

   if(count >= word1.length()) 
      return;

   else
   {      
      for(int i = 0; i < word2.length(); i++){         
         if (word1.charAt(count) == word2.charAt(i)){

         word1_unicode = word1check.codePointAt(count);
         char_code[count] = word1_unicode;  

         temp[i] = 0;
         String str = new String(temp);
         word2 = str;

         word2_unicode = word2check.codePointAt(i);
         char_code2[count] = word2_unicode; 

   if(count==word1.length()-1)
     break;

  search(word1, word2, ++count, char_code, char_code2, match, temp);

     }
  }   

}
  return;
}
}

最佳答案

您的问题发生在您正在使用的递归中,而不是您认为的地方。在调用“search”方法之前,您需要增加计数变量。

search(word1, word2, ++count, char_code, char_code2, match);

解决此问题的最简单方法是在调用自身方法之前添加检查

if(count==word1.length()-1)
    break;
search(word1, word2, ++count, char_code, char_code2, match);

这样,如果计数已到达单词末尾,您就不会调用搜索方法,并且它永远不会通过越界来打破它。

我在测试过程中发现的另一个问题在这里

for(int i = 0; i < word1.length()-1; i++)

这样,当您搜索时,您将永远不会到达第二个单词的末尾,并且如果您的第二个单词以在单词中仅使用一次的字符结尾,则它永远不会进入此处

if (word1.charAt(count) == word2.charAt(i)){
// I think the problem is right around here, but I don't know what to change 

    word1_unicode = word1check.codePointAt(count);
    char_code[count] = word1_unicode; 

    if(char_code2[count] == 0) { //prevents double counting of letters
        word2_unicode = word2check.codePointAt(i);
        char_code2[count] = word2_unicode;
        search(word1, word2, ++count, char_code, char_code2, match);
    } 

    if((count==0)&&(i == word1.length()-1)){
        match = false;
    }
}

为了解决这个问题,我只是删除了“for”循环中的“-1”。

此方法中的最后一个奇怪的问题是,它实际上返回一个您在程序开始时预定义的 boolean 类型,但它实际上从未达到返回 false 的程度。此时我发现你的重复计算预防也不起作用。基本上大约一半的线路并没有真正做任何事情。

希望我能帮助您解决我在程序中发现的问题。此时,如果第一个单词中的所有字母都可以在第二个单词中找到,则会说 2 个单词是字谜词。我能想到的防止重复计算单词的最简单方法就是简单地覆盖此位置的字母。

下面的代码实际上与我的防止重复计算字母的想法配合得很好:

import java.util.Scanner;

public class test {

public static void main(String[] args) {

System.out.println("Please enter two words, one per line, to test if it is an anagram");
Scanner userInput = new Scanner(System.in);
String word1 = userInput.nextLine();
String word2 = userInput.nextLine();
int count = 0;
boolean match = true;

    if (word1.length() == word2.length()){
        match = search(word1, word2, count,match);
    }
    else {
        match = false;
    }

    if(match)
        System.out.println("The words are anagrams");
    else
        System.out.println("The words are not anagrams");
}

    public static boolean search(String word1, String word2, int count,boolean match)
    {

        if(count >= word1.length()-1) 
            return match;

        else
        {
            for(int i = 0; i < word1.length(); i++)
            { 

                if (word1.charAt(count) == word2.charAt(i)){
                    char[] temp = word2.toCharArray();
                    temp[i] = 0;
                    word2 = temp.toString();
                    search(word1, word2, ++count, match);
                } 
                else
                    match = false;
            }
        }
        return match;

    }
}

关于java - 样式(递归和越界异常已得到解答,这是新的),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13433872/

相关文章:

添加项目时Java ArrayList OutofBounds

java - 用 Canvas 绘制人物并设置其角度的最佳方法是什么?

C# 客户端和 Java 服务器

c - 这个递归函数让我百思不得其解,这是怎么回事?

recursion - 左/右递归和 Bison 解析堆栈行为

java - 在 JSP 页面上显示树

python - python 中的 anagram 列表列表

java - 在 Spring Boot 应用程序中加载第三方 jar 的问题

c++ - 从过去的工作面试中检查我的字谜代码

php - 使用 PHP 编写一个字谜函数?