Newbie
Newbie

Reputation: 89

Passing array data to three methods to find average, max and minimum

I want to be able to display the output as three lines of text at the end but I'm getting a really weird output. So for example

"The average is z"

"Min Value: x"

"Max Value: y"

But I am getting this:

Please enter the size of the array: 3

Please enter array elements: 10

The average is 3.

Min Value: 0

Please enter array elements: 20

The average is 10.

Max Value: 20

Min Value: 0

Please enter array elements: 30

The average is 20.

Max Value: 20

Max Value: 30

package arrays;
import java.util.Scanner;
public class AssignmentArrays {

public static void main (String[] args) {
      Scanner inputs = new Scanner(System.in);
      
        System.out.println("Please enter the size of the array: ");
        int size = inputs.nextInt();
     
        int[] Array = new int[size]; 
 
        for (int i = 0; i < size;) {
            System.out.println("Please enter array elements: ");
            int value = inputs.nextInt();
            if (value >=100 || value <=0) {
                System.out.println("Only values greater than 0 and less than 100 can be accepted.");
            } 
            else {
                Array[i] = value;
                i++;
average(Array); 
getMaxValue(Array); 
getMinValue(Array); 
} 
        } 
} 
public static int average(int[] Array)
{
        int sum = 0;
        int average; 
        for (int i = 0; i < Array.length; i++)
            sum =  sum+Array[i];
        { 
        average=sum/Array.length; 
        System.out.println("The average is " +average +".");
        return average;
} 
} 
public static int getMaxValue(int[] Array) {
    int maxValue = Array[0];
    for (int i = 1; i < Array.length; i++){
        if (Array[i] > maxValue){
            maxValue = Array[i];
            System.out.println("Max Value: " + maxValue);
        }
    }
    return maxValue;
    
}

public static int getMinValue(int[] Array) {
    int minValue = Array[0];
    for (int i = 1; i < Array.length; i++){
        if (Array[i] < minValue) {
            minValue = Array[i];
            System.out.println("Min Value: " + minValue);
        }
    }
    return minValue;
    
}

}

Upvotes: 1

Views: 216

Answers (1)

Newbie
Newbie

Reputation: 89

Thank you for all your help! I have finished my assignment.

example output: https://i.sstatic.net/Doy2d.png :)

package arrays;
import java.util.Scanner; 
public class Array_1 {
public static void main(String[] args) {
    // TODO Auto-generated method stub
      Scanner inputs = new Scanner(System.in);
      
        System.out.println("Please enter the size of the array: ");
        int size = inputs.nextInt();
     
        int[] Array = new int[size]; 
 
        for (int i = 0; i < size;) {
            System.out.println("Please enter array elements: ");
            int value = inputs.nextInt();
            if (value >=100 || value <=0) {
                System.out.println("Only values greater than 0 and less than 100 can be accepted.");
            } 
            else {
                Array[i] = value;
                i++;                
            }
            }
            
            Average(Array); 
            
            int max;
            max = Max(Array); 
            System.out.println("The maximum is " +max + ".");
            
            int min;
            min = Min(Array); 
            System.out.println("The minimum is " +min + ".");
            
        } 

        public static void Average(int[] Array) {
        int sum = 0;
        int average; 
        for (int i = 0; i < Array.length; i++)
            sum =  sum+Array[i];
        average=sum/Array.length; 
        System.out.println("The average is " +average +".");}    

public static int Max(int[]Array) { 
        int max= Array[0];
        for (int i = 1; i < Array.length; i++) {
            if (Array[i] > max) {
                max = Array[i];
            }
        }
        return max;
} 

public static int Min(int[]Array) { 
    int min = Array[0];
    for (int i = 1; i < Array.length; i++) {
        if (Array[i] < min) {
            min = Array[i];
        }
    }
    return min;

Upvotes: 1

Related Questions