How to report data in a vector

2

I can not report 10 student grades.

import java.util.Scanner;

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

        double[] N = new double[10];
        int i;

        for(i=0; i<N.length; i++){
            System.out.println("Informe a Nota");
            N[] = scn.nextDouble();

        }
    }
}
    
asked by anonymous 11.09.2017 / 08:31

2 answers

1

You have created a vetor of 10 positions, so you must enter in which position you want to write a value.

Example

String[] Carros = new String[3];
Carros[2] = "Gol"; // A última posição vai conter o valor: Gol
Carros[0] = "HB20"; // A Primeira posição vai conter o valor: HB20

The solution to your problem is to change the line:

N[] = scn.nextDouble();

To:

N[i] = scn.nextDouble();

Reference: A little bit of arrays

    
11.09.2017 / 10:00
1

You should use a variable as an index just as the name itself tells the index to vary with each passing through the loop. In case what is varying according to the loop is the variable i . You have slightly different ways of doing this.

I took advantage of and improved some things:

import java.util.Scanner;

class exer {
    public static void main(String[] args){
        Scanner scn = new Scanner(System.in);
        double[] N = new double[10];
        for (int i = 0; i < N.length; i++) {
            System.out.println("Informe a Nota");
            N[i] = scn.nextDouble();
        }
    }
}

See running on ideone . And in Coding Ground . Also I placed GitHub for future reference .

    
11.09.2017 / 11:37