样式(已回答递归和越界异常,这是新的)

Posted

技术标签:

【中文标题】样式(已回答递归和越界异常,这是新的)【英文标题】:Style (recursion & out of bounds exception was answered, this is new) 【发布时间】:2012-11-06 04:45:55 【问题描述】:

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

这是修改后的版本:

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

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

我想让它看起来尽可能直截了当,但我得到的真正建议很少。所以如果有人有任何想法..

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;


【问题讨论】:

首先要改变的是缩进:缩进你的代码,这样很容易看到执行流程。 谢谢,我就是这么做的。 为什么要对非递归任务使用递归?线性方法(计算相同长度单词的每个字符的实例)编码起来会更简单,速度也会更快。 你说得对,在这个项目中,我更感兴趣的是看看这些不同的组件如何组合在一起,而不是最大限度地提高效率。我认为我放弃了使用递归的想法,因为我无法让程序做我需要做的事情。 【参考方案1】:

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

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

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

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

这样,如果 count 已到达单词的末尾,您将不会调用 search 方法,并且它永远不会通过超出范围来破坏它。

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

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”。

这个方法的最后一个奇怪的问题是它实际上返回了一个你在程序开始时预定义的布尔类型,它实际上从来没有达到得到假的程度。在这一点上,我发现您的双重计数预防也不起作用。基本上大约一半的行并没有真正做些什么。

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

这里的代码实际上可以很好地配合我防止重复计算字母的想法:

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;

    

【讨论】:

谢谢,您对 .toCharArray 的使用非常巧妙。我在将其转换为字符串时遇到了一些麻烦,不确定 .toString() 发生了什么或它是什么问题,但不用担心 - 我为那部分使用了不同的解决方案。我的程序现在运行得很好,它可以比较 unicode 值并使用它来确定它是否是一个字谜。非常感谢您的意见!我一直在写字谜程序的工作日,而这个程序尤其具有挑战性。我感谢您的帮助。我很高兴它有效! 很高兴我能帮助您为双叉字母提供不同的解决方案,但发现您的解决方案比它需要的更复杂,尽管其他想法也可以。

以上是关于样式(已回答递归和越界异常,这是新的)的主要内容,如果未能解决你的问题,请参考以下文章

将MySQL的查询语句放到Mybatis的XML文件中报参数越界异常

作业10-异常

作业10-异常

系统如何识别问题是不是已被回答

查找不属于特定用户的文件

pascal exitcode=255 是啥错误?