gpt4 book ai didi

JAVA:对象数组仅打印最后一个元素

转载 作者:行者123 更新时间:2023-12-01 20:55:38 25 4
gpt4 key购买 nike

我的数据和算法类(class)作业告诉我们创建一个系统任务工单程序。它要求我们做各种事情,例如向数组添加新的 SystemTask 对象、删除任务以及列出/打印排序的对象数组。

我已经完成了大部分编码,但我一直在打印对象数组。看来无论我添加到对象数组中的最后一个数据是什么,它都会成为数组中所有元素的数据。

Some instructions:

  • My WorkOrders() constructor must place the 4 default SystemTask objects in the array.
  • listSystemTask() method lists all SystemTask objects in the array (in order) with a number in front.

NOTE: Please refrain from telling me to use List or any advance java methods. My homework has limitations to it.

系统任务类

  public class SystemTask{

private static String month, message;
private static int day, hour, minute;

//constructor methods
public SystemTask(){
month = "Jan";
day = 1;
hour = 00;
minute = 00;
message = "Task Here";
}

public SystemTask(String mon, int d, int h, int mnt, String msg){
month = mon;
day = d;
hour = h;
minute = mnt;
message = msg;

/*setMonth(mon);
setDay(d);
setHour(h);
setMinute(mnt);
setMessage(msg);*/
}

//get and set methods
public static String getMonth(){
return month;
}
public static int getDay(){
return day;
}
public static int getHour(){
return hour;
}
public static int getMinute(){
return minute;
}
public static String getMessage(){
return message;
}
public static void setMonth (String mon){
if(mon.length()!= 3)
System.out.println("Invalid 3 Letter Code. Try Again.");
else
month = mon;
}
public static void setDay(int d){
if(d<1 || d>31)
System.out.println("Invalid Day. Try Again.");
else
day = d;
}
public static void setHour(int h){
if(h<0 || h>23)
System.out.println("Invalid Hour. Try Again.");
else
hour = h;
}
public static void setMinute(int mnt){
if(mnt<0 || mnt>59)
System.out.println("Invalid Minute. Try Again.");
else
minute = mnt;
}
public static void setMessage(String msg){
if(msg.length()<1 || msg.length()>40)
System.out.println("Invalid Message. Try Again.");
else
message = msg;
}

@Override
public String toString(){
return getMonth()+" "+getDay()+", "+String.format("%02d",getHour())
+":"+String.format("%02d",getMinute())+" "+getMessage();
}
}

工单类

public class WorkOrders {

private final SystemTask[] ary = new SystemTask[20]; //declare private 20 objects array
//public static int index, empty;

public static void main(String args[]){
WorkOrders object = new WorkOrders(); //create a WorkOrders object
object.run(); //call a run method
}

public WorkOrders(){
// for(int i = 0;i<ary.length;i++){
// ary[i] = new SystemTask();
// }

/*SystemTask t1 = new SystemTask("Mar", 4, 21, 30, "Backup Users");
ary[0] = t1;
SystemTask t2 = new SystemTask("Apr", 1, 17, 0, "Upgrade Hard Drives");
ary[1] = t2;
SystemTask t3 = new SystemTask("May", 6, 10, 45, "Virus Scan");
ary[2] = t3;
SystemTask t4 = new SystemTask("Jun", 3, 9, 15, "Database Backup");
ary[3] = t4;*/

ary[0] = new SystemTask("Mar", 4, 21, 30, "Backup Users");
ary[1] = new SystemTask("Apr", 1, 17, 0, "Upgrade Hard Drives");
ary[2] = new SystemTask("May", 6, 10, 45, "Virus Scan");
ary[3] = new SystemTask("Jun", 3, 9, 15, "Database Backup");
}

public void run(){
char choice;
do{
System.out.println("SYSTEM WORKORDER PROGRAM:");
System.out.println("A)dd SystemTask");
System.out.println("D)elete SystemTask");
System.out.println("L)ist SystemTask");
System.out.println("E)xit");
System.out.print("Select an option: ");
choice = UserInput.getChar();

switch (choice) {
case 'a':
case 'A':
//addSystemTask();
break;
case 'd':
case 'D':
//deleteSystemTask();
break;
case 'l':
case 'L':
listSystemTask();
break;
case 'e':
case 'E':
System.out.println("You quit the program.");
System.out.println("\nThanks for using System Workorder!");
System.exit(0);
break;
default:
System.out.println("\'" + choice + "\' does not exist. Try again.");
break;
}
System.out.println();
}while(choice !='E'|| choice !='e');
}

public void listSystemTask(){
System.out.println("\nALL LISTED TASKS");
for(int c=0;c<ary.length;c++){
if(ary[c]!=null)
System.out.println(c+1 + ": " + ary[c].toString());
}
}

我制作了另一个 UserInput 类。我没有包含它,因为它仅用于我的键盘输入。我省略了一些与我的问题无关的代码/方法。我只关心数组中对象的打印。

这里的问题是,这是我打印列表时得到的输出:

SYSTEM WORKORDER PROGRAM:
A)dd SystemTask
D)elete SystemTask
L)ist SystemTask
E)xit
Select an option: L

ALL LISTED TASKS
1: Jun 3, 09:15 Database Backup
2: Jun 3, 09:15 Database Backup
3: Jun 3, 09:15 Database Backup
4: Jun 3, 09:15 Database Backup

不知道问题出在哪里。我怀疑它是在构造函数中数组的初始化中,但我似乎无法修复它。或者也许它在其他地方?

最佳答案

问题出在 SystemTask

public class SystemTask {
private static String month, message;
private static int day, hour, minute;

您已将所有成员变量声明为静态。将有多个 SystemTask 实例,但它们都将其值存储在相同的 static 字段中。它打印“最后一个 SystemTask”,因为它是最后一个创建的,覆盖了之前设置的值。

只需删除 static 声明...

public class SystemTask {
private String month, message;
private int day, hour, minute;

您还将所有访问器方法定义为静态。也从其中删除 static,以允许它们访问对象范围变量。

  public static String getMonth() {
return month;
}

成为...

  public String getMonth() {
return month;
}

关于JAVA:对象数组仅打印最后一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42430269/

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