-4

I want to calculate the average numbers using arrays. I want the program asks for the amount of grades and after I want to put the grade numbers.

After I want to get the average output in a double.

This is my code so far:

public class Average {

    public static void main(String[] args) 
    {
        //int n = MyConsole.readInt("Enter number of grades: " );

        int a = MyConsole.readInt("Enter grade 1: " );
        int b = MyConsole.readInt("Enter grade 2: " );
        int c = MyConsole.readInt("Enter grade 3: " );

        int[] numbers = new int[]{a,b,c};
        numbers[0] = a;
        numbers[1] = b;
        numbers[2] = c;


        int sum = 0;

        for(int i=0; i < numbers.length ; i++)
                sum = sum + numbers[i];

        double average = sum / numbers.length;

        System.out.println("Average value of array elements is : " + average);
    }
}
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
itay izraelov
  • 47
  • 1
  • 1
  • 5
  • 2
    `numbers[0] = a; numbers[0] = b; numbers[0] = c;` this is two things: ***wrong***, because you're always writing to the same index and ***obsolete***, because you already initialized your array correctly with `a`, `b` and `c`. So you can remove these lines. – Tom Apr 15 '16 at 10:54
  • 3
    What do you want exactly? to be able to do this with any number of grades? – dquijada Apr 15 '16 at 10:58
  • Actually for me it´s quite unclear what you are asking. Could you prove a sample input and the expected output? – SomeJavaGuy Apr 15 '16 at 10:59
  • 1
    yes. I want to choose the number of grades. – itay izraelov Apr 15 '16 at 11:00
  • So why did you comment out the input for the amount of grades that you do want to input? Just create the proper `int` array with the size and let the user input the grades in a loop. – SomeJavaGuy Apr 15 '16 at 11:02
  • 4
    The OP is obviously new to Stackoverflow. So a note to all downvoters, a better approach would be to tell him what is wrong with the question, instead of downvoting it. – SexyBeast Apr 15 '16 at 11:06

5 Answers5

5

Don't know what your class MyConsole is doing, but I guess is a Scanner:

Your code improved will be something like this:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    System.out.print("Enter number of grades: " );
    int n = sc.nextInt();

    int sum = 0;

    for (int i = 0; i < n; i++) {
        System.out.print("Enter grade "+ (i + 1) + ": ");
        int a = sc.nextInt();
        sum += a;
    }

    double average = sum / n;

    System.out.println("Average value of array elements is : " + average);
}

OUTPUT (2 grades):

Enter number of grades: 2
Enter grade 1: 1
Enter grade 2: 5
Average value of array elements is : 3.0

OUTPUT (5 grades):

Enter number of grades: 5
Enter grade 1: 10
Enter grade 2: 20
Enter grade 3: 30
Enter grade 4: 10
Enter grade 5: 50
Average value of array elements is : 24.0

NOTE

double average = sum / n;

performs an int division, so you won't have any decimal places! I would propose a fast cast:

double average = sum / (double) n;

With new output:

Enter number of grades: 2
Enter grade 1: 1
Enter grade 2: 4
Average value of array elements is : 2.5

GUESS using your own class:

public static void main(String[] args) {
    int sum = 0;

    int n = MyConsole.readInt("Enter number of grades: " );

    for (int i = 0; i < n; i++) {
        int a = MyConsole.readInt("Enter grade "+ (i + 1) + ": ");
        sum += a;
    }

    double average = sum / n;

    System.out.println("Average value of array elements is : " + average);
Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
1

thank you ! Sorry for the poor explanation. This is my first question

this it the code after edit:

import java.util.Scanner;

public class Average {

    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter number of grades: ");
        int n = sc.nextInt();
        int sum = 0;

        int[] numbers = new int[n];

        for(int i=0; i < numbers.length ; i++)
        {
            System.out.println("Enter grade " + (i + 1) + " :");
            int a = sc.nextInt();
            sum = sum + a;
        }

        double average = sum / (double) n;

        System.out.println("Average value of array elements is : " + average);
        sc.close();
    }
}
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
itay izraelov
  • 47
  • 1
  • 1
  • 5
0

Program to Calculate Average Using Arrays:

public class Inter1 { //name of the class

public static void main(String[] args) {//main method 

int  number[]={40,56,23,56,87,23,78}; //declaring  the int array
int sum=0;
for (int s:number){ //for each 
    sum +=s;
}
int ave=sum/number.length; //to get the average
    System.out.println("the average is "+ave); //out put 
 }    
}
0
public class Inter1 { //name of the class

    public static void main(String[] args) { //main method

        System.out.println("==============================");

        int num[]={34,56,78,78,34,2,33,99,100,56}; //int array 
        int total=0;
        for (int i=0;i<num.length;i++){ //for loop
            total+=num[i];
        }

        int avrage1=total/num.length; //output
                System.out.println("The average is "+avrage1);

     }    
}
Mahdi Ghiasi
  • 14,873
  • 19
  • 71
  • 119
0
package inter1;

import static java.time.Clock.system; import java.util.Scanner;

public class Inter1 {

public static void main(String[] args) {
Scanner in = new Scanner (System.in);
int total=0;
    System.out.println("Enter how many number that do u wanna enter ?? ");
    int num= in.nextInt();
    int numbers[]=new int[num];
    for (int i=0;i<numbers.length;i++){
        System.out.println(i+1+":"+"enter the your numbers ? ");
        numbers[i]=in.nextInt();
    }
    for (int i=0;i<numbers.length;i++){
        total+=numbers[i];
    }
    int average =total/numbers.length;
    System.out.println("the average is "+average);

 }    
}