首页 > 解决方案 > 返回空值

问题描述

我想显示一些值。

我想在屏幕上显示学生插入的笔记。首先,我插入笔记的数量,然后我写笔记(int),最后,我返回笔记的值。问题是当返回注释值时:返回 null。

在此处输入图像描述

类注释:

package notas;

public class Notas {
    private int identificador;
    private String texto;

    public Notas(int identificador, String texto) {
        this.identificador = identificador;
        this.texto = texto;
    }

    public int getIdentificador() {
        return identificador;
    }

    public void setIdentificador(int identificador) {
        this.identificador = identificador;
    }

    public String getTexto() {
        return texto;
    }

    public void setTexto(String texto) {
        this.texto = texto;
    }

    @Override
    public String toString() {
        return "Nota{" +
                "identificador=" + identificador +
                ", texto='" + texto + '\'' +
                '}';
    }
}

Blocnotas 类:

package notas;
import java.util.*;

public class BlocNotas extends Notas{
    public int tamano;
    public String[] notas;
    Scanner entrada = new Scanner(System.in);

    public BlocNotas(int identificador, String texto) {
        super(identificador, texto);
    }
    
    public int getTamano() {
        return tamano;
    }

    public void setTamano(int tamano) {
        this.tamano = tamano;
    }

    public String[] getNotas() {
        return notas;
    }

    public void setNotas(String[] notas) {
        this.notas = notas;
    }

    public void cantidadNotas(){
        System.out.print("¿Cuántas notas va a introducir? ");
        tamano = entrada.nextInt();
    }

    public void introducirNotas(){
        String[] notas = new String[getTamano()];
        int i;
        System.out.println("Introduzca sus "+tamano+" notas..");
        for(i=0; i<tamano; i++){
            System.out.print("Inserte su nota n"+(i+1)+": ");
            notas[i] = entrada.next();
        }
    }

    public void listarNotas(){
        String[] notas = new String[getTamano()];
        System.out.println("Aquí puede ver las notas del bloc de notas:");
        int i;
        for(i=0; i<tamano; i++){
            System.out.print(notas[i]+" ");
        }
    }
}

一个例子(Main.java):

package notas;
import notas.BlocNotas;
import notas.Notas;

public class NewMain {
    public static void main(String[] args) {
        BlocNotas bn = new BlocNotas(1, "Maths");
        bn.cantidadNotas();
        bn.introducirNotas();
        bn.listarNotas();
    }
}

标签: java

解决方案


您已经在introducirNotas()listarNotas()方法中分别创建了String[] notas作为本地数组。该String[] notas本地数组中包含的值不会替换全局String[] notas数组。

因此,在插入值时,应将具有这些值的数组分配给全局String[] notas数组。

设置注释(注释);在此处输入图像描述

public void introducirNotas(){
    String[] notas = new String[getTamano()];
    int i;
    System.out.println("Introduzca sus "+tamano+" notas..");
    for(i=0; i<tamano; i++){
        System.out.print("Inserte su nota n"+(i+1)+": ");
        notas[i] = entrada.next();
    }
    setNotas(notas);
}

检索值时,必须将具有这些值的全局String[] notas数组分配给本地数组。

字符串[] notas = getNotas();

public void listarNotas(){
    String[] notas = getNotas();
    System.out.println("Aquí puede ver las notas del bloc de notas:");
    int i;
    for(i=0; i<tamano; i++){
        System.out.print(notas[i]+" ");
    }
}

推荐阅读