How to include a new note in an array?

Asked

Viewed 142 times

0

Make a program that calculates the arithmetic mean of N banknotes using array.

I’m trying to do this:

package exer4;

import java.util.Scanner;

public class Exer4 {

   public static void main(String[] args){

   int nota[] = {4,5,7,4,3,6,6,8,3,4};
       int perguntar = in.nextInt();
   int aluno = 0;
   int soma = 0;

   while(aluno < 10){
    System.out.println("Aluno: " + aluno + " Nota: " + nota[aluno]);
    aluno++;
   }
   for(int i = 0; i < nota.length; i++){
    soma = soma + nota[i];

    System.out.println(soma);
   }        

}
} 

I did it that way:

package exer4;

import java.util.Scanner;

public class Exer4 {

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

    int nota[] = {1,4,5,7,6, inserir};
        int inserir = in.nextInt();
        System.out.println("Digite as suas notas e espere a media");
        System.out.println("Sua nota pode ser:" + nota);
        System.out.println("Sua media pode ser:" + nota / 2);

    }
 }  

I’m trying this other way too:

package exer4;

import java.util.Scanner;

public class Exer4 {

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

    int nota[] = {1,4,5,7,6, inserir};
        nota[0] = in.nextInt();
        System.out.println("Digite as suas notas e espere a media");
        System.out.println("Sua nota pode ser:" + nota);
        System.out.println("Sua media pode ser:" + nota / 2);

    }
 }  
  • 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 (when you have enough score).

1 answer

2

You can’t. You have some options, create a array well large enough for all of them, which can cause an unnecessary memory consumption, but that does not make real difference in many cases, even more in Java and for an exercise. Or do what everyone else does which is use a list instead of a array, which is usually recommended even if you don’t need to add anything. I’m just not going to say that in Java there is no disadvantage because it is a verbose language and requires writing much more code to manipulate a list than a array, but in other languages has no real disadvantage sensitive, and has advantages. Another alternative would be to recreate the array bigger and copying everything from one to the other every time it doesn’t fit new elements, which at the bottom is what the list does, only it does it in a well thought out way that you probably won’t do.

A array cannot grow in size, once it creates it gets that size.

However, the statement does not ask to add new items in array, he asks only to create a array with a certain amount of elements. Then ask how many elements you will have, such as N that the statement speaks and then create the array of the necessary size.

All codes seem to have things kind of meaningless like initializing a array that will not use these values, or ask for a data that is not used, or prefer a while where a for works better, or use an extra loop where the operation can be solved in the same loop already used before and did not average, besides printing the one accumulating at each step, anyway, the code is pretty chaotic. The second and third code make no sense.

It would be something like that:

import java.util.Scanner;

class Exer4 {
    public static void main(String[] args) {
        Scanner in = new Scanner (System.in);
        int alunos = in.nextInt();
        int notas[] = new int[alunos];
        int soma = 0;
        for (int i = 0; i < alunos; i++) {
            notas[i] = in.nextInt();
            soma += notas[i];
        }
        System.out.println(soma / alunos);
    }
}

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

Browser other questions tagged

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