java - 返回数字,但打印为 0

标签 java arrays recursion printing return

好吧,我一直在开发这个程序,它需要使用递归方法,并且数组编号必须是数组而不是 ArrayList。该程序的目的是允许用户输入 double ,直到输入 0。 double 必须存储到数组中,并且我必须使用递归方法来查找最大值、负数的数量以及正数的总和。我遇到的问题是当程序退出并打印结果时,它打印最大值和总和为 0 而不是答案。我已经尝试了几乎所有我能想到的方法,但无法使其发挥作用。如果有人可以告诉我我的问题是什么,我将不胜感激......谢谢。

注意:首先,我将 max、negative 和 sum 放入到 assignment9 方法中,并尝试从 main 方法中进行打印,但似乎没有任何效果。

import java.util.*;
import java.text.*;;

public class Assignment9 {

//main method initializes variables then calls method assignment9
public static void main(String[] args)
{
    int count = 0;
    ArrayList<Double> help = new ArrayList<Double>();
    double max = 0;
    int negative = 0;
    double sum = 0;
    assignment9(help, count, max, negative, sum);

}//end main

//adds user input to array until 0 is entered; it then calls other methods
public static void assignment9(ArrayList<Double> help, int count, double max, int negative, double sum)
{
    DecimalFormat fmt = new DecimalFormat("#.##");
    double input;

    Scanner scan = new Scanner(System.in);

    input = scan.nextInt();

    if (input == 0)
    {
        double[] numbers = new double[count];
        for(int i = 0; i < numbers.length; i++)
        {
            numbers[i] = help.get(i);
        }
        findMax(numbers, count-1, max);
        countNegative(numbers, count-1, negative);
        computeSumPositive(numbers, count-1, sum);

    }else{
            help.add(input);
            count++;
            assignment9(help, count, max, negative, sum);
    }//end if
    System.out.println("The maximum number is " + fmt.format(max));
    System.out.println("The total number of negative numbers is " + negative);
    System.out.println("The sum of positive numbers is " + fmt.format(sum));
    System.exit(0);

}//end assignment9

//compares elements of array to find the max until count = -1
public static double findMax(double[] numbers, int count, double max)
{

    if (count == -1)
    {
        return max;
    }else if(numbers[count] > max){
        max = numbers[count];
        count--;
        findMax(numbers, count, max);
    }//end if
    return max;

}//end findMax

public static int countNegative(double[] numbers, int count, int negative)
{

    if(count == -1)
    {
        return negative;
    }else if(numbers[count] < 0){
        negative++;
        count--;
        countNegative(numbers, count, negative);
    }
    return negative;
}//end countNegative

public static double computeSumPositive(double[] numbers, int count, double sum)
{
     if(count == -1)
     {
         return sum;
     }else{
         sum = sum + numbers[count];
         count--;
         computeSumPositive(numbers, count, sum);
         return sum;
     }
}//end computeSumPositive

}//end class

最佳答案

当您打印出行中的值时

System.out.println("The maximum number is " + fmt.format(max));

您永远不会将 max 变量分配给 assignment9 方法中的任何内容。当然,您可以调用 findMax,但在此之前您从未说过 max = findMax(...)。由于您在 main 中通过传入 0 来调用它,所以这就是它打印出来的内容。

你的 max 函数还有一些其他的 bug,但这就是它总是打印 0 的原因。

关于java - 返回数字,但打印为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15713756/

相关文章:

haskell - 如何存储和引用递归函数的中间值

java - 从 Java 代码运行 Python 脚本

java - 在 Java 中查找 4 个复选框的有序选择

iphone - 在 iOS 6 的 TableView 中对数组进行排序

php - 在php中检查数组是否为空

python - 切片 numpy 数组时使用 python 的逻辑运算符

Javascript - 为什么下面的函数定义出错?

ruby - 从字符串中删除不匹配的括号

java - 使用 hibernate 截断所有表的最佳方法?

java - 在java中传递一个抽象类而不是一个对象?