0

わかりましたので、再帰メソッドを使用する必要があるこのプログラムに取り組んでおり、配列番号はArrayListではなく配列でなければなりません。このプログラムの目的は、ユーザーが 0 を入力するまで double を入力できるようにすることです。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
4

1 に答える 1

1

次の行の値を出力すると、

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

max変数をassignment9メソッド内の何かに割り当てることはありません。確かに、あなたは電話するかもしれfindMaxませんが、その前に決して言いませんmax = findMax(...). mainで 0 を渡して 呼び出したので、それが出力されます。

あなたの max 関数には他のバグがありますが、これが常に 0 を出力する理由です。

于 2013-03-30T01:19:01.877 に答える