简体   繁体   English

代码可以编译,但是我所有的答案都是0

[英]The code compiles, but all my answers come out to be 0

The code compiles, but all my answers come out to be 0. 代码可以编译,但是我的所有答案都是0。

import java.util.Scanner;// use the Scanner class located in the "java.util" directory

public class Assignment2
{
    public static void main(String[] args)//main method that runs the program using methods below
    {
        double[] numbers = new double[100];
        Scanner input = new Scanner(System.in);
        int count;
        int c=0;
        do  
        {
            count = input.nextInt();
            c++;       
        } while (count != 0 && c < numbers.length);

        double min = findMin(numbers, numbers.length);
        int countNeg = countNegative(numbers, numbers.length);
        double sum = computePositiveSum(numbers, numbers.length);
        System.out.println("The minimum number is " +min);
        System.out.println("The sum of the positive numbers is $" +countNeg) ;
        System.out.println("The total number of negative numbers is " +sum);
    }

    public static double findMin(double[] numbers, int count) //finds and displays the minimum input value
    {
        double min = numbers[0];
        for(count =1; count<numbers.length; count++)
        {
            if(numbers[count] <min)
            {
                min = numbers[count];
            }
        }
        return min;
    }
    public static int countNegative (double[] numbers, int count) //counts the number of times a negative number is added to the array
    {
        int countNeg =0;
        for (count = 0; count < numbers.length; count++)
        {
            if(numbers[count] < 0)
            {
                countNeg = countNeg + 1;
            }   
        }
        return countNeg;
    }

    public static double computePositiveSum(double[] numbers, int count)//calculates the sum of the positive integers in the array
    {
        double sum = 0;
        for(count=0; count<numbers.length; count++)
        {
            if(numbers[count] > 0)
            {
                sum = sum + numbers[count];
            }
        }
        return sum;
    }
}

You are not assigning the entered values to the array numbers . 您没有将输入的值分配给数组numbers Add this 加上这个

numbers[c] = count;

inside the do-while loop: do-while循环中:

do {
    count = input.nextInt();
    numbers[c] = count;
    c++;
} while (count != 0 && c < numbers.length);

Note that if you just input positive (> 0) numbers and don't fill the whole array numbers , findMin() will return 0 , because it is the default value for the elements of the array. 请注意 ,如果您仅输入正数(> 0)而没有填写整个数组numbers ,则findMin()将返回0 ,因为它是数组元素的默认值。

Edit: 编辑:

I would say that the print statement 我会说印刷声明

System.out.println("The sum of the positive numbers is $" + countNeg);
System.out.println("The total number of negative numbers is " + sum);

are not coherent with the methods called. 与所调用的方法不一致。 It should be: 它应该是:

System.out.println("The sum of the positive numbers is " + sum);
System.out.println("The total number of negative numbers is " + countNeg);
        double[] numbers = new double[100];
        Scanner input = new Scanner(System.in);
        int count;
        int c=0;
        do  
        {
            count = input.nextDouble();
            numbers[c] = count;
            c++;       
        } while (count != 0 && c < numbers.length);

This code should work. 此代码应该起作用。 You have to add the numbers to the array which you are entering through stdin 您必须将数字添加到要通过stdin输入的数组中

double[] numbers = new double[100];

Here you are initializing the array with all 0.0 elements. 在这里,您将使用所有0.0元素初始化数组。 You want to take input int he array from stdin? 您想从stdin数组输入数据吗?

You don't put anything into numbers array so they are all 0. The corrected code is this: 您无需将任何内容放入number数组中,因此它们全为0。更正后的代码是这样的:

    do  
    {
        numbers[c] = input.nextDouble();
        c++;       
    } while (numbers[c] != 0 && c < numbers.length);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM