java - 返回空值
问题描述
我想显示一些值。
我想在屏幕上显示学生插入的笔记。首先,我插入笔记的数量,然后我写笔记(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();
}
}
解决方案
您已经在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]+" ");
}
}
推荐阅读
- python - 如何解析 SEC EDGAR 文件
- python - Kaldi 是否返回任何识别置信度参数,类似于 Google Speech-To-Text API?
- ruby-on-rails - 如何使用 jquery 依赖项使用 webpacker 在 Rails 6 上安装 inputmask
- c# - 我怎样才能得到空的对象(如图所示)
- wordpress-gutenberg - 如何将 js 插件渲染到 Gutenberg Editor 的后端
- aws-lambda - aws 注册 InvalidLambdaResponseException
- javascript - React - “在专用组件中呈现列表”是否避免协调?
- sql - 检查 MariaDB 字符串值中的第一个小写字符
- java - 使用 Excel 在 Drools 规则决策表中添加多个参数以执行操作
- php - 外键失败 laravel