Java递归代码未返回预期结果

时间:2018-10-15 01:41:01

标签: java recursion

下面是不返回时间以及预期值的代码。我在调试消息中包含了输出。感谢您抽出宝贵的时间回顾我的工作。

package module4;

import java.nio.charset.spi.CharsetProvider;
import java.util.Scanner;

public class LabRecursive{

    public static void main(String[] args) {

        // Prompt user and call the method
        Scanner s = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String var = s.next();
        System.out.println("There is/are " + vowels(var, 0, 0) + " vowel(s) in the string " + var);
        s.close();

    }

    public static int vowels(String input, int currentPos, int amount) {
        // Create the arrays of chars to hold vowels and the input
        System.out.println("Amount at beginning: " + amount);
        char[] vow = {'a', 'e', 'i', 'o', 'u'};        
        char[] entered = input.toLowerCase().toCharArray();
        int stringLength = input.length();

        System.out.println("stringlength " + stringLength);

        if (currentPos < stringLength) {
            for (int i=0; i<5; i++) {
                if (entered[currentPos] == vow[i]) {
                    amount++;
                    System.out.println("vowel: " + entered[currentPos]);
                    System.out.println("amount: " + amount);
                    continue;
                }
            }

                currentPos++;
                System.out.println("Amount before calling the recursive function: " + amount);
                System.out.println("currentPos before calling the recursive function: " + currentPos);
                vowels(input, currentPos, amount);       
        }

        System.out.println("Amount before returning the final amount: " + amount);
        return amount;
    }   
}
  

输入一个字符串:ijo

     

开始金额:0

     

stringlength 3

     

元音:我

     

数量:1

     

调用递归函数之前的金额:1

     

currentPos,然后调用递归函数:1

     

开始金额:1

     

stringlength 3

     

调用递归函数之前的金额:1

     

currentPos,然后调用递归函数:2

     

开始金额:1

     

stringlength 3

     

元音:o

     

数量:2

     

调用递归函数之前的金额:2

     

currentPos,然后调用递归函数:3

     

开始金额:2

     

stringlength 3

     

返回最终金额之前的金额:2

     

返回最终金额之前的金额:2

     

返回最终金额之前的金额:1

     

返回最终金额之前的金额:1

     

ijo字符串中有1个元音

2 个答案:

答案 0 :(得分:0)

此更改应解决此问题。

vowels(input, currentPos, amount); 

amount = vowels(input, currentPos, amount);       

答案 1 :(得分:0)

为了提高速度并更好地使用递归方法。
所有输入参数甚至都是只读的。

public static int vowels(final String input, final int currentPos, final int amount) {
    // Create the arrays of chars to hold vowels and the input
    System.out.println("Amount at beginning: " + amount);
    char[] vow = {'a', 'e', 'i', 'o', 'u'};        
    char[] entered = input.toLowerCase().toCharArray();
    int stringLength = input.length();

    System.out.println("stringlength " + stringLength);

    if (currentPos < stringLength) {
        for (int i=0; i<5; i++) {
            if (entered[currentPos] == vow[i]) {
                System.out.println("vowel: " + entered[currentPos]);
                System.out.println("amount: " + amount);
                return vowels(input, currentPos + 1, amount + 1); 
            }
        }

        System.out.println("Amount before calling the recursive function: " + amount);
        System.out.println("currentPos before calling the recursive function: " + currentPos);
        return vowels(input, currentPos + 1, amount);       
    }

    System.out.println("Amount before returning the final amount: " + amount);
    return amount;
}