How to inform data in a vector

Asked

Viewed 51 times

2

I’m not getting to 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();

        }
    }
}
  • Just a Note: you are not following the Java naming convention, Class names must start with uppercase letter (Exer instead of exer), and, variable names start with lowercase letter (n instead of N).

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

  • I answered yes that it worked. Vlw, but I’m a beginner here so I don’t frequent much hehe

  • You’ve done the tour, @user90625 ? It teaches you to accept the questions. Click on the green "v" that appears just below the vote. You earn even 2 reputation points by accepting a question

2 answers

1

You created a vetor of 10 positions, then you must indicate in which position you wish 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

  • but in that case, each vector is already defined, eh?? I need to enter the data and fill in the vector of 10 position :( ??

  • There is only one example, inside the for alter N[] for N[i] that will work as you wish.

1

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

I took advantage 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();
        }
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • vlw! thank you very much

  • @user90625 see on [tour] the best way to say thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.