简体   繁体   中英

Accepting input through an array in Java

I'm trying to have the user enter an array of numbers(=marks of students). Also, the user himself will decide the size of the array, ie, the number of students in class. Having accepted the marks of each student, i want to store them in an array, display the marks of all students, and compute the average of the class. Will get to the average part later. Can someone help me with this (apparently wrong) piece of code, thank you!!

package chapter2.Arrays;
import java.util.*;

public class Arrays_ExampleOne {

static int size;
static int marks;
static int [] numbers=new int [size];
static Scanner in=new Scanner(System.in);


public static void main(String[] args) {
    // TODO Auto-generated method stub

    System.out.println("Welcome to Cincinnati Elementary");
    System.out.println("Class report details for ");
    System.out.println("Enter the number of students in the class");
    size=in.nextInt();
    System.out.println("Enter the marks of each student");
    for(int i=0;i<=size;i++)
    {
    numbers[i]=in.nextInt();    
    }

/*  System.out.println("The marks of all students are"+ " ");
    {
    for(int i=0;i<=size;i++)
    {
        System.out.print(numbers[i]);
    }
    System.out.println();

    }
    //int avg=
    */}
}

===========================================================

Revised code, looking to create a function outside of main to calculate an array's average:

package chapter2.Arrays;
import java.util.*;

public class Arrays_ExampleOne {

/*static int size;
static int marks;
static int [] numbers=new int [size];*/
static Scanner in=new Scanner(System.in);


    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.out.println("Welcome to Cincinnati Elementary");
        System.out.println("Class report details for ");
        System.out.println("Enter the number of students in the class");
        int size;
        //static int marks;
        //static int [] numbers=new int [size];

        size=in.nextInt();

        int [] numbers=new int [size];
        System.out.println("Enter the marks of each student");
        for(int i=0;i<size;i++)
        {
        numbers[i]=in.nextInt();    
        }

        System.out.println("The marks of all students are:"+ " ");
        {
        for(int temp:numbers)
        {
            System.out.print(temp+ " ");
        }
        System.out.println();
        }

        int arraySize=numbers.length;
        int arraySum=0;
        //Calculate the sum of array elements
        for(int temp1:numbers)
        {
            arraySum+=temp1;
        }

        int average=arraySum/arraySize;

        System.out.println("The average of all the students scores is:"+ " "+average);
    }

    /*public int calculateAverage(int array[])
    {
        int arraySize=array.length;

    }*/
}

============================================================== Added separate code for calculating Average, outside of main.

package chapter2.Arrays;
import java.util.*;

public class Arrays_ExampleOne {

/*static int size;
static int marks;
static int [] numbers=new int [size];*/
static Scanner in=new Scanner(System.in);


    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Arrays_ExampleOne obj=new Arrays_ExampleOne();
        System.out.println("Welcome to Cincinnati Elementary");
        System.out.println("Class report details for ");
        System.out.println("Enter the number of students in the class");
        int size;
        //static int marks;
        //static int [] numbers=new int [size];

        size=in.nextInt();

        int [] numbers=new int [size];
        System.out.println("Enter the marks of each student");
        for(int i=0;i<size;i++)
        {
        numbers[i]=in.nextInt();    
        }

        System.out.println("The marks of all students are:"+ " ");
        {
        for(int temp:numbers)
        {
            System.out.print(temp+ " ");
        }
        System.out.println();
        }

    /*  int arraySize=numbers.length;
        int arraySum=0;
        //Calculate the sum of array elements
        for(int temp1:numbers)
        {
            arraySum+=temp1;
        }

        int average=arraySum/arraySize;

        System.out.println("The average of all the students scores is:"+ " "+average);*/
        int average=obj.calculateAverage(numbers);
        System.out.println("The average of all the students is:"+ " "+average);
    }

    public int calculateAverage(int array[])
    {
        int arraySize=array.length;
        int arraySum=0;
        for(int temp: array)
        {
        arraySum+=temp;
        }

        int average=arraySum/arraySize;

        return average;
    }
}

Firstly, you should ask user to input the size first, then you set the array size:

static int size;
static int marks;
static Scanner in=new Scanner(System.in);

public static void main(String[] args) {
    System.out.println("Enter number of student:");
    size = in.nextInt();
    int [] numbers=new int [size];

    //the rest of your code goes here
}

Note: Be careful with your for loop:

for(int i=0;i<=size;i++){
             ^
    numbers[i]=in.nextInt();
}

It will result ArrayIndexOutOfBoundException since you are trying to access the element at index size , which the max index of array with n size is n-1 . Should be < instead of <=

For method calculateAverage, it should looks like this:

public static double calculateAverage(int array[])
{
    double sum=0.0;
    for(int i=0;i<array.length;i++){
       sum+=array[i];
    }
    return sum / array.length;
}

note that this method should be static since you want to use it inside static method, and average always deals with double / float datatype, not int

From main method, you can call it like this:

System.out.println("Average marks:"+calculateAverage(numbers));
static int size;
static int marks;
static int [] numbers=new int [size];

At the time the array is created, size is still at the default value, which is zero. You always end up with an array of length zero here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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