Variable arrangement in Java

4

I need to make the length of the array declared by the user running the program and not established within the code, this is what I'm doing but I got confused and I do not know what to do as soon as I'm learning how to program.

import java.util.Scanner;

/* */
class UN {

private double Calificacion[];
private int j;
private int x[];

public UN() {

    Calificacion = new double[];
}

public void setCalificacion() {

    System.out.println(" Introduce las calificaciones ");
    Scanner dato = new Scanner(System.in);
    for (int j = 0; j < Calificacion.length; j++) {
        Calificacion[j] = dato.nextDouble();
    }

}

public double getCalificacion() {
    for (int j = 0; j < Calificacion.length; j++) {
        System.out.println(Calificacion[j]);

    }
    return Calificacion[j];
}

}

public class MensajeDeArreglos {

public int x;

public static void main(String[] args) {

    System.out.println(" Establece la longitud del arreglo ");
    Scanner dato = new Scanner(System.in);


    UN uno = new UN();
    double Calificacion[];
    Calificacion = new double[];
    System.out.println(" la longitud del arreglo es " + Calificacion.length);
    uno.setCalificacion();
    System.out.println(" Las calificaciones establecidas son: ");
    uno.getCalificacion();

}
}
    
asked by Andrei Ar 05.04.2018 в 02:47
source

1 answer

3

In this case it is only necessary that you put the code of the declarations and the setCalificacion method.

For your question, try the following:

public UN() {

    double[] Calificacion;
    //variable del tamaño del array
    int icant;
}

public void setCalificacion() {

    System.out.println(" Introduce las cantidad de calificaciones");
    Scanner cant = new Scanner(System.in);
    //Conversión de scanner a int
    icant = cant.nextInt();
    //Declaración del array con el tamaño definido por el usuario
    Calificacion = new double[icant];
    System.out.println(" Introduce las calificaciones ");
    Scanner dato = new Scanner(System.in);
    for (int j = 0; j < Calificacion.length; j++) {
        Calificacion[j] = dato.nextDouble();
    }

}
    
answered by 05.04.2018 в 03:50