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

以下是不返回我预期的时间和内容的代码。我已经包含了调试消息的输出。感谢您花时间回顾我所做的一切。


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


字符串长度 3


元音:我


数量:1


调用递归函数前的量:1


调用递归函数前的 currentPos: 1


开始数量:1


字符串长度 3


调用递归函数前的量:1


调用递归函数前的 currentPos:2


开始数量:1


字符串长度 3


元音:o


数量:2


调用递归函数前的数量:2


调用递归函数前的 currentPos:3


开始数量:2


字符串长度 3


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


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


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


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


字符串 ijo 中有 1 个元音


隔江千里
浏览 264回答 2
2回答

PIPIONE

此更改应该解决此问题。vowels(input,&nbsp;currentPos,&nbsp;amount);到amount&nbsp;=&nbsp;vowels(input,&nbsp;currentPos,&nbsp;amount);

红颜莎娜

提高速度并更好地使用递归方法。所有输入参数甚至可以是只读的。public static int vowels(final String input, final int currentPos, final int amount) {&nbsp; &nbsp; // Create the arrays of chars to hold vowels and the input&nbsp; &nbsp; System.out.println("Amount at beginning: " + amount);&nbsp; &nbsp; char[] vow = {'a', 'e', 'i', 'o', 'u'};&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; char[] entered = input.toLowerCase().toCharArray();&nbsp; &nbsp; int stringLength = input.length();&nbsp; &nbsp; System.out.println("stringlength " + stringLength);&nbsp; &nbsp; if (currentPos < stringLength) {&nbsp; &nbsp; &nbsp; &nbsp; for (int i=0; i<5; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (entered[currentPos] == vow[i]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("vowel: " + entered[currentPos]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("amount: " + amount);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return vowels(input, currentPos + 1, amount + 1);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Amount before calling the recursive function: " + amount);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("currentPos before calling the recursive function: " + currentPos);&nbsp; &nbsp; &nbsp; &nbsp; return vowels(input, currentPos + 1, amount);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println("Amount before returning the final amount: " + amount);&nbsp; &nbsp; return amount;}&nbsp; &nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java