-4

Only using three methods (including main) and an array of 10 whole numbers, I need to create a program that does these stuff:

  1. Count how many people who passed the exam.
  2. Print the statistics. (eg. grade A, B, C, D, E...)
  3. How many people passed or failed their exam.

Here's what I have right now. The only problem I have is that I don't know how to return multiple values.

public class Array10PassFail

public static int countPass (int [] num)
{
    int count = 0;
    int counter = 0;
for (int i = 0; i < num.length; i++)
    {
        if (num [i] >= 50)
        {
            count++;
        }
        else if (num [i] < 50)
        {
            counter++;
        }
    }
 return count;
}



public static int printStats (int [] num)
{
    int aTotal = 0, bTotal = 0,cTotal = 0, dTotal = 0, eTotal = 0;
    for (int i = 0; i < num.length; i++)
    {
        if (num[i] >= 80)
        {
            aTotal++;
        }
        else if (num[i] >= 70)
        {
            bTotal++;
        }
        else if (num[i] >= 60)
        {
            cTotal++;
        }
        else if (num[i] >= 50)
        {
            dTotal++; 
        }
        else if (num[i] < 50)
        {
            eTotal++; 
        }
    }
    return aTotal;
}

public static void main (String [] args)

{
    Scanner sc = new Scanner(System.in); 

    int [] num = new int [10];

    for (int i = 0; i < num.length; i++)
    {
        System.out.print("Enter score: "); 
        num[i] = sc.nextInt(); 
    }

    int passf = countPass(num);

    System.out.println("There are " + passf + " people who passed and " + ??? + " who failed. "); 
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875

2 Answers2

0

If you want to return multiple values, you usually want to return a single object that encapsulates the values instead. Use a List> or an ArrayList, add your values into the List and return it. In calling function, get the return value in List and extract the info as you see fit

0

Create an array. For example, the

public static int[] countPass(int[] num){
    int count = 0, counter=0;
    for (int i = 0; i < num.length; i++){
        if (num [i] >= 50) count++;
        else counter++;
    }
    return new int[]{count,counter};
}

and the doing something like

int array[] = countPass(num);
System.out.println("Failed -> " + array[1] + "Passed->" + array[0]);

Similarly, do it for the grades.

dumbPotato21
  • 5,669
  • 5
  • 21
  • 34