存储对象到数组帮助Java [英] Storing Object Into Array Help Java
问题描述
我试图做一个预约项目,你可以添加,显示,删除约会。
I am trying to make a appointment project which you can add, display, delete appointments.
我有三类: UserInput
,预约
,计划
。
-
UserInput
:只是推动用户进入任何(INT
,双
等) -
预约
:已set和get方法。两个构造,的toString
和方法,该用户的UserInput
类,然后使用set方法与输入的值。 -
计划
:在课堂上,我不得不删除,显示和添加约会的选项。
UserInput
: is just to promote the user to enter whatever (int
,double
, etc..)Appointment
: has set and get methods. two constructor,toString
, and a method that user theUserInput
class and then use the set methods with the value entered.Planner
: is the class I have the options to delete, display and add appointments.
我创建预约
的数组对象,在计划类20个插槽私人约会[]数组=新约会[20];。
I created an array object of Appointment
with 20 slots in the planner class.private Appointment[] array = new Appointment[20];
我现在面临的问题是将对象添加到阵列。我试图在构造函数中的一些插槽分配默认值。我显示了阵列和它的工作。现在我做了调用方法和分配曾被输入什么 inputAppointment
(即要求用户输入然后调用设置方法的类)的方法。
The problem I am facing is with adding objects to the array. I tried assigning default value for some slots in the constructor. I displayed the array and it worked. Now I made a method to call the inputAppointment
( the class that ask the user to input then call the set methods ) method and assign what ever was inputted.
该方法看起来像
public void addAppointment() {
Appointment object = new Appointment();
object.inputAppoitment();
array[5] = object;
}
inputAppointment方法
inputAppointment method
public void inputAppoitment() {
UserInput Object = new UserInput();
System.out.println("Enter The Month");
setMonth(Object.getString(3, 3));
System.out.println("Enter The Day");
setDay(Object.getInt(1, 31));
System.out.println("Enter The Hour");
setHour(Object.getInt(1, 24));
System.out.println("Enter The Minute");
setMinute(Object.getInt(1, 59));
System.out.println("Enter A Short Description");
setMessage(Object.getString(5, 40));
}
我跑了方法,那么显示的数组,但加入什么!
我可以做似乎太复杂,但如果你想要的东西更清楚我将很高兴来解释。
I ran the method then displayed the array but nothing was added! I may seem made it too complicated but if you want something more clear I would be happy to explain.
更新:
我打过电话从主不是我的菜单循环的方法,它似乎工作。
这是我的菜单看起来怎么样。
Update: I tried calling the method from main not my menu loop and it seems to work. this is how my menu look like.
while (true) {
System.out.println("\nPlease Choose.");
System.out.println("A)dd Appointment , D)elete Appointment , L)ist Appointment , E)xit");
char userChoice = UserInput.getChar();
Planner obj = new Planner();
if (userChoice == 'A' || userChoice == 'a') {
obj.addAppointment();
}
if (userChoice == 'D') {
}
if (userChoice == 'L') {
obj.listAppointment();
}
if (userChoice == 'E') {
break;
}
}
谢谢大家。
推荐答案
问题是对象的范围。
public void addAppointment() {
// this would be unreferenced when the method finishes
Appointment object = new Appointment();
object.inputAppoitment();
array[5] = object; // so now array[5] refers to object;
} // object is no longer in scope, array[5] no longer has a reference to it
我会重写你的code是这样的:
I would rewrite your code like this:
public Appointment addAppointment() {
// we have to access the reference when the method returns
Appointment object = new Appointment();
object.inputAppoitment();
return object; // we return the reference to object and asign it into array[5]
}
我们叫它是这样的:
array[5] = addAppointment();
这篇关于存储对象到数组帮助Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!