【问题标题】:Why does my method return the wrong value?为什么我的方法返回错误的值?
【发布时间】:2019-02-25 17:11:24
【问题描述】:

尽管我的方法 operationsNeeded 为我的 return-int "count1" 打印了正确的值,但在下一行它会向我的 main 方法返回其他内容。我没有包含我的其余代码,如果需要,我很乐意提供。 例如,如果 operationsNeeded 被执行了 4 次,count1 在 4 上也会被打印出来。但由于我不知道的原因 System.out.println("check: " +count1);语句执行 4 次,如下所示:
检查:4
检查:4
检查:3
检查:2

我希望我的程序只执行一次,然后继续返回语句。

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    int testcases = sc.nextInt();
    int count =0;
    while (count<testcases){
        int numberOfColleagues = sc.nextInt();
        sc.nextLine();
        String startPieces = sc.nextLine();

        int[] listOfcolleagues = listOfColleagues(numberOfColleagues, startPieces);

        int count2 = operationsNeeded(listOfcolleagues, 1);
        count++;
        System.out.println(count2);
    }

}

 public static int operationsNeeded (int[] listOfColleagues, int count1){
    //remove duplicates first
    ArrayList<Integer> relevantList=removeDuplicatesAndSort(listOfColleagues);
    System.out.println("relevantlist" + relevantList);
    //check for smallestdelta & index
    int [] deltaAndIndex = smallestDeltaHigherIndex(relevantList);
    int delta = deltaAndIndex[0];
    int index = deltaAndIndex[1];

    if (delta==1){
        for (int i=0;i<relevantList.size();i++){
            if (i!=index){
                relevantList.set(i,relevantList.get(i)+1);
            }

        }
    }
    if (delta>1 && delta<5){
        for (int i=0;i<relevantList.size();i++){
            if (i!=index){
                relevantList.set(i,relevantList.get(i)+2);
            }
        }
    }
    if (delta>4){
        for (int i=0;i<relevantList.size();i++){
            if (i!=index){
                relevantList.set(i,relevantList.get(i)+5);
            }
        }
    }

    System.out.println(count1);
    int[] updatedList = new int[relevantList.size()];
    for (int i=0; i<relevantList.size();i++){
        updatedList[i]=relevantList.get(i);
    }
    if (!isAllTheSame(relevantList)) {
        count1 +=1;
        operationsNeeded(updatedList,count1);
    }

        System.out.println("check: " + count1);

 return count1;
}

【问题讨论】:

    标签: java return


    【解决方案1】:

    您的方法是递归的。 "check: " 行打印在该递归的每个级别上,并带有它当前在该级别上的值。它首先打印“最内层”的值 (4),然后打印上一层的值(也是 4),最后打印顶层的 hte 值,在上面的 if 中递增后为 2。而它returns的值始终是从顶层到顶层的值。

    如果您只想打印一次,您可以只在最内层打印它,使用else。但是,这仍将是来自顶级迭代的return 值;相反,跟踪从递归调用返回的值并相应地更新count1

    if (! isAllTheSame(relevantList)) {
        // we have to go deeper!
        count1 = operationsNeeded(updatedList, count1 + 1);
    } else {
        // phew, finally done
        System.out.println("check: " + count1);
    }
    

    【讨论】:

    • 谢谢。我编辑了我的代码:if (!isAllTheSame(relevantList)) { count1++; count1=operationsNeeded(updatedList,count1); } return count1; } 现在它可以完美运行了!我想,因为我在 return 语句之前调用了 operationsNeeded 函数,所以在我的 if 条件为真之前我不会到达 return 语句。你介意指出我的想法的错误吗?
    • @Louis 好吧,一旦递归调用完成,它仍然会到达调用函数的末尾。这样,递归调用与其他方法调用没有任何不同。
    猜你喜欢
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 2023-03-18
    • 2020-04-19
    • 2021-04-10
    • 1970-01-01
    相关资源
    最近更新 更多