java语言中一种具有逻辑错误的递归插入方法

46qrfjad  于 2023-01-19  发布在  Java
关注(0)|答案(1)|浏览(91)

我做了一个递归的插入方法,在一个升序的数字列表中插入一个数字,并保留列表顺序。它很简单,也很有效,问题是当我想插入的数字比列表中的所有数字都高时,它不会返回正确的列表并复制它
这是我的代码:

public static String insertNumber(String list, int numberToInsert, int startOfList, int endOflist) {
    if (startOfList > endOflist) { // base case, reach end of the list 
        return list;
    }
    if (list.charAt(startOfList) == numberToInsert + '0') { // number already exist in list
        System.out.println("No changes because " + numberToInsert + " exist in the list");
        System.exit(0);
    }
    if (list.charAt(startOfList) > numberToInsert + '0') {
        String s = Character.toString((char) (numberToInsert + '0'));
        list = s + list.substring(startOfList);
        return list;
    }
    return list.charAt(startOfList) + insertNumber(list, numberToInsert, startOfList + 1, endOflist);
}

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter a numerical ascending list and a number to insert: ");

    String inputList = scan.next();
    int numberToInsert = scan.nextInt();

    String result = insertNumber(inputList, numberToInsert, 0, inputList.length() - 1);
    System.out.println("The new list is: " + result);
}

这是针对以下输入得到的输出:

Enter a numerical ascending list and a number to insert: 
12567 3
The new list is: 123567 

----------------------------------------------
Enter a numerical ascending list and a number to insert: 
4567 1
The new list is: 14567

----------------------------------------------
Enter a numerical ascending list and a number to insert: 
3456 9 
The new list is: 34563456

我不知道我哪里做错了。请帮帮我

wmvff8tz

wmvff8tz1#

您的"基本用例"返回错误的结果。测试用例在处理完整列表后命中该用例,startOfList为4,endOfList为3。它返回整个列表,该列表被追加到已处理的整个列表中。
如果它返回"numberToInsert"(作为一个字符串),它将正常工作:

if (startOfList > endOflist) { // base case, reach end of the list 
    return ""+numberToInsert;
}

结果:

Enter a numerical ascending list and a number to insert: 
3456 9
The new list is: 34569

完整更新代码:

public static String insertNumber(String list, int numberToInsert, int startOfList, int endOflist) {
        if (startOfList > endOflist) { // base case, reach end of the list 
            return ""+numberToInsert;
        }
        if (list.charAt(startOfList) == numberToInsert + '0') { // number already exist in list
            System.out.println("No changes because " + numberToInsert + " exist in the list");
            System.exit(0);
        }
        if (list.charAt(startOfList) > numberToInsert + '0') {
            String s = Character.toString((char) (numberToInsert + '0'));
            list = s + list.substring(startOfList);
            return list;
        }
        return list.charAt(startOfList) + insertNumber(list, numberToInsert, startOfList + 1, endOflist);
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter a numerical ascending list and a number to insert: ");

        String inputList = scan.next();
        int numberToInsert = scan.nextInt();

        String result = insertNumber(inputList, numberToInsert, 0, inputList.length() - 1);
        System.out.println("The new list is: " + result);
    }

相关问题