递归辅助方法 [英] Recursive helper method

查看:114
本文介绍了递归辅助方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到适合本练习的正确解决方案,这是任务:

i cant find the right solution for this exercise, here is the task:

(在数组中出现指定字符)编写一个递归方法 查找数组中指定字符的出现次数. 您需要 定义以下两种方法.第二种是递归辅助方法.

(Occurrences of a specified character in an array) Write a recursive method that finds the number of occurrences of a specified character in an array. You need to define the following two methods. The second one is a recursive helper method.

公共静态整数计数(char []字符,字符ch)

public static int count(char[] chars, char ch)

公共静态int计数(char []字符,char ch,int高)

public static int count(char[] chars, char ch, int high)

编写一个测试程序,提示用户在一行中输入字符列表, 和一个字符,并在列表中显示该字符的出现次数.

Write a test program that prompts the user to enter a list of characters in one line, and a character, and displays the number of occurrences of the character in the list.

1)仅当添加另一个参数(整数索引)时,我才能解决该问题,但是如何在不添加另一个参数或不使用for循环的情况下解决呢?

1) I can solve it only if I add another parameter (int index) but how can I do it without adding another parameter or using for loop ?

2)为什么那里有辅助方法?我不了解递归中辅助方法的用途.

2)Why is the helper method there? I don't understand the purpose of helper methods in recursion.

这是我的解决方案:

package occurencesinarray;

import java.util.Scanner;

public class Start {
public static void main(String[] args){
    System.out.println("Enter few characters: ");
    Scanner scan = new Scanner(System.in);
    String s = scan.nextLine();
    char[] chars = new char[s.length()];
    for(int i = 0; i < s.length(); i++){
        chars[i] = s.charAt(i);
    }
    System.out.println("Enter desired character: ");
    char ch = scan.nextLine().charAt(0);

    System.out.println(count(chars, ch));
}

public static int count(char[] chars, char ch){
    return count(chars, ch, 0, 0);
}

public static int count(char[] chars, char ch, int high, int index){
    if(index == chars.length){
        return high;
    }
    if(chars[index] == ch){
        return count(chars, ch, high + 1, index + 1);
    } else{
        return count(chars, ch, high, index + 1);
    }
}
}

推荐答案

正如AllenKll指出的那样,high值可能应该扮演您打算用于index的角色.您一直在统计high变量中的出现次数,但是此计数可以在递归中隐藏".

As AllenKll already pointed out, the high value should probably take the role that you intended for your index. You have been counting the number of occurances in the high variable, but this counting can be "hidden" in the recursion.

这些用于递归的辅助"方法的目的通常恰好是:它们通常具有(至少)一个附加参数,该参数以某种方式描述了递归已经进行了多长时间或仍必须进行多远.作为后者的一个示例:您还可以通过编写

The purpose of these "helper" methods for recursion in general is exactly that: They usually have (at least) one additional parameter that somehow describes how far the recursion has already proceeded or how far it still has to proceed. As an example for the latter: You could also have used the high variable as a "countdown", by writing

public static int count(char[] chars, char ch)
{
    return count(chars, ch, chars.length - 1);
}

public static int count(char[] chars, char ch, int high)
{
    if (high == -1)
    {
        return 0;
    }
    if (chars[high] == ch)
    {
        return 1 + count(chars, ch, high - 1);
    }
    return count(chars, ch, high - 1);
}

当然,只能提供帮助方法.不用打电话

Of course, one could only offer the helper method. Instead of calling

count(chars, ch);

您可以要求用户致电

count(chars, ch, 0);

但是这里的问题是此方法可能被滥用:当用户随后将错误的值作为最后一个参数传递时,该方法将无法工作.

But the problem here is that this method may be misused: When then user passes a wrong value as the last parameter, then the method will not work.

注意:整个助手方法"只有在助手方法为私有时才有意义.当它是public时,用户可能仍然调用错误的方法.我看到任务说明中要求使用public修饰符,但是...当您让讲师意识到这一缺陷时,也许您会得到一些加分;-)

Note: This whole "helper method" thing only makes sense when the helper method is private. When it is public, the user may still call the wrong method. I see that the public modifier was requested in the task description, but... maybe you'll receive some bonus points when you make your instructor aware of this flaw ;-)

这篇关于递归辅助方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆