gpt4 book ai didi

java - 实例化对象用于显示另一个类中存储的信息

转载 作者:行者123 更新时间:2023-12-01 18:51:10 24 4
gpt4 key购买 nike

我实例化了一个类,以便我将输入的信息存储到其中。但我很难显示存储的信息。当我编译时它会说我还没有初始化变量。

    public class REPORTS
{
public static void main(String[]args)
{
Scanner input = new Scanner(System.in);
int x=choices();

STUDENT stud;
EQUIPMENT equip;
RESERVATION reserve;
switch(x)
{
case 1:
{
// Code here for input

stud = new STUDENT(Studid,Studname,Studcourse,Studlevel);

break;
}
case 2:
{
// Code also here for input
equip = new EQUIPMENT(eqpmntid,qty,eqpmntname);
break;
}
case 3:
{
// Same goes for here input
reserve = new RESERVATION(studentid,equipid1,reservationdate,returndate);
break;
}
case 4:
{

stud.display(); // error here variable might not have been initialized
break;
}
case 5:
{

equip.display(); // same goes here
break;
}
case 6:
{
reserve.display(); // and also here
break;
}
}

这是详细的错误消息:

This is the error

最佳答案

看来你必须改变你的程序行为;有一种可能的语法方法可以在代码开头将新对象转换为 Student,但您可能会收到运行时错误。要更改行为,您可以根据构造函数获取的参数,使用 null 或空的参数或虚拟对象来初始化对象,然后在相关 switch case 中将字段设置为适当的值;例如,您的第一行应如下所示:

Student stud = new Student("", Null, dummyObject);

关于java - 实例化对象用于显示另一个类中存储的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59737448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com