从单个方法返回多个值[重复]

时间:2022-02-04 02:00:57

This question already has an answer here:

这个问题在这里已有答案:

I had a question in my exam. At first it was pretty easy. Let me explain the question:

我的考试中有一个问题。起初它非常简单。让我解释一下这个问题:

  • Make the User Specify how many integers wants to put (Array)

    使用户指定要放入的整数(数组)

  • Numbers should be between -1500 to 1500

    数字应介于-1500到1500之间

  • (Tricky): Create a method that calculates the percentage of the numbers put, what percentage were 0, positive, negative. (All this should be in ONE method, no return to string, string buffer, the return result should be a double value)
  • (整蛊):创建一个方法来计算所放数字的百分比,百分比是0,正数,负数。 (所有这一切应该在ONE方法中,不返回字符串,字符串缓冲区,返回结果应该是double值)

I think I did it right, but when I tested it in real time, the problem that I run is that it returns the values 0.0, 0.0, 0.0 ... Or it returns the correct percentage only if the values fall within the same condition (eg. all zero).

我认为我做得对,但是当我实时测试它时,我运行的问题是它返回值0.0,0.0,0.0 ......或者只有当值落在相同条件下时才返回正确的百分比(例如,全部为零)。

Here is my code: (I hope you can make sense out of it). I'm just curious, I don't know how to solve it. I have also tried it with a static method, I run into the same problem.

这是我的代码:(我希望你可以理解它)。我只是好奇,我不知道如何解决它。我也尝试过静态方法,遇到同样的问题。

import java.util.Scanner;

public class nPNZ {
    public int [] number;

    public nPNZ (int [] n){
        number = n;
    }

    public double [] allCalcs(){
        int countPos = 0; int countNeg = 0; int countZero = 0;
        for (int i = 0; i < number.length; i++) {
            if (number[i] == 0) {
                countZero++;
            }else if (number[i] > 0){
                countPos++;
            }else{
                countNeg++;
            }
        }
        //0 = 0 ; 1 = positive ; 2 = negative
        double total [] = new double [3];
        total[0] = (countZero/number.length)*100;
        total[1] = (countPos/number.length)*100;
        total[2] = (countNeg/number.length)*100;
        return total;
    }

    public static void main (String args[]){
        //min 20 number, -1500 to 1500
        Scanner input = new Scanner (System.in);

        final int MIN_SIZE = 5;
        int size = 0;

        while(size < MIN_SIZE){
            System.out.println("Specify the size of the array: ");
            size = input.nextInt();
            while(size<MIN_SIZE){
                System.out.println("Size should be greater than: " + MIN_SIZE);
                size = input.nextInt();
            }
        }

        input.nextLine();

        int num [] = new int [size];

        for (int i = 0; i < num.length; i++) {
            System.out.println("Write number " + (i+1) + " : ");
            num[i] = input.nextInt();
            while(num[i] < -1500 || num[i] > 1500) {
                System.out.println("The number should within the range of -1500      and 1500");
                num[i] = input.nextInt();
            }
        }

        nPNZ n = new nPNZ (num);
        System.out.println("Percentage of zero numbers is: " + n.allCalcs()[0] );
        System.out.println("Percentage of positive numbers is: " + n.allCalcs()[1] );
        System.out.println("Percentage of negative numbers is: " + n.allCalcs()[2]);
    }
}

1 个解决方案

#1


1  

I can see one problem in your code.

我可以在你的代码中看到一个问题。

double total [] = new double [3];
total[0] = (countZero/number.length)*100;
total[1] = (countPos/number.length)*100;
total[2] = (countNeg/number.length)*100;
return total;

Here, countZero, countPos and countNeg are all integers and number.length is also integer. So, when you divide an integer by another integer, you will get an integer.

这里,countZero,countPos和countNeg都是整数,number.length也是整数。因此,当您将整数除以另一个整数时,您将得到一个整数。

Since, countZero, countPos and countNeg are all less than number.length, you will get zero value in the total array.

因为countZero,countPos和countNeg都小于number.length,所以你将在整个数组中获得零值。

Example:

System.out.println(2/3); // prints 0
System.out.println(2.0/3); // prints 0.6666666666666666
System.out.println((double)2/3); // prints 0.6666666666666666

There are several alternatives to solve your problem. One of them is, simply multiply the integer by 1.0 while dividing it by another integer.

有几种方法可以解决您的问题。其中之一是,简单地将整数乘以1.0,同时将其除以另一个整数。

You can do something like this.

你可以做这样的事情。

public double [] allCalcs(){
    // your code goes here
    double total [] = new double [3];
    total[0] = (countZero * 1.0 / number.length) * 100;
    total[1] = (countPos * 1.0 / number.length) * 100;
    total[2] = (countNeg * 1.0 / number.length) * 100;
    return total;
}

OR, you can declare the variables (countZero, countPos and countNeg) as double.

或者,您可以将变量(countZero,countPos和countNeg)声明为double。

#1


1  

I can see one problem in your code.

我可以在你的代码中看到一个问题。

double total [] = new double [3];
total[0] = (countZero/number.length)*100;
total[1] = (countPos/number.length)*100;
total[2] = (countNeg/number.length)*100;
return total;

Here, countZero, countPos and countNeg are all integers and number.length is also integer. So, when you divide an integer by another integer, you will get an integer.

这里,countZero,countPos和countNeg都是整数,number.length也是整数。因此,当您将整数除以另一个整数时,您将得到一个整数。

Since, countZero, countPos and countNeg are all less than number.length, you will get zero value in the total array.

因为countZero,countPos和countNeg都小于number.length,所以你将在整个数组中获得零值。

Example:

System.out.println(2/3); // prints 0
System.out.println(2.0/3); // prints 0.6666666666666666
System.out.println((double)2/3); // prints 0.6666666666666666

There are several alternatives to solve your problem. One of them is, simply multiply the integer by 1.0 while dividing it by another integer.

有几种方法可以解决您的问题。其中之一是,简单地将整数乘以1.0,同时将其除以另一个整数。

You can do something like this.

你可以做这样的事情。

public double [] allCalcs(){
    // your code goes here
    double total [] = new double [3];
    total[0] = (countZero * 1.0 / number.length) * 100;
    total[1] = (countPos * 1.0 / number.length) * 100;
    total[2] = (countNeg * 1.0 / number.length) * 100;
    return total;
}

OR, you can declare the variables (countZero, countPos and countNeg) as double.

或者,您可以将变量(countZero,countPos和countNeg)声明为double。