简体   繁体   English

使用 for 循环和 object 方法将值分配给 Java 中的对象数组时遇到问题

[英]Trouble assigning values to an array of objects in Java, using a for loop and object methods

In my Time class i have:在我的时间class 我有:

public Time(String start_time) 
    {
    time = start_time;
    hrs = Integer.parseInt(time.substring(0, 2));
    mins = Integer.parseInt(time.substring(3, 5));
    secs = Integer.parseInt(time.substring(6, 8));
    }

and

public void addHour()
  {
    hrs = hrs + 1;

    }
    if (hrs > 23) {
      hrs = hrs - 24;
    }

    prntsecs = String.valueOf(secs);
    prntmins = String.valueOf(mins);
    prnthrs = String.valueOf(hrs);

    if (secs < 10) {
      prntsecs = "0" + String.valueOf(secs);
    }
    if (mins < 10) {
      prntmins = "0" + String.valueOf(mins);
    }
    if (hrs < 10) {
      prnthrs = "0" + String.valueOf(hrs);
    }

    time = prnthrs + ":" + prntmins + ":" + prntsecs;
  }

In my Main class i have:在我的主要class 我有:

Time tstamps[] = new Time[5];
Time a = new Time("09:00:00");
for (int i = 0; i < 5; i++){
  tstamps[i] = a;
  a.addHour();
}

tstamps[0].printTime();
tstamps[1].printTime();
tstamps[2].printTime();
tstamps[3].printTime();
tstamps[4].printTime();

I want it to print:我希望它打印:

09:00:00 09:00:00
10:00:00 10:00:00
11:00:00 11:00:00
12:00:00 12:00:00
13:00:00 13:00:00

But it prints:但它打印:

14:00:00 14:00:00
14:00:00 14:00:00
14:00:00 14:00:00
14:00:00 14:00:00
14:00:00 14:00:00

I've tried messing around with the code in my Main class, but I'm very much a beginner, please help me understand my mistake and how to fix it...我试过弄乱我的 Main class 中的代码,但我是个初学者,请帮助我理解我的错误以及如何解决它......

Edit:编辑:

Sorry, I still don't quite understand how to do this...抱歉,我还是不太明白怎么做……

I tried to follow your advice and did this:我试着听从你的建议并做到了:

Time tstamps[] = new Time[5];
    for (int i = 0; i < 5; i++){
      Time a = new Time("09:00:00");
      for (int j = 1; j < i; j++){
        a.addHour();
      }
      tstamps[i] = a;
    }

But now it just prints 09:00:00 five times, instead of 14:00:00.但现在它只打印了五次 09:00:00,而不是 14:00:00。

My field definitions for the Time class:我对时间 class 的字段定义:

public String time;
public String prntsecs;
public String prntmins;
public String prnthrs;
public int hrs;
public int mins;
public int secs;

I don't have any for the Main class.我没有任何主要的 class。

Also, please excuse me if I break any community norms, or something of the sort, this is my second time ever posting on StackOverflow.另外,如果我违反任何社区规范或类似的东西,请原谅,这是我第二次在 StackOverflow 上发帖。

Based upon your posted code - but removing the compile error from addHour根据您发布的代码 - 但从addHour中删除编译错误

and modifying to并修改为

    Time tstamps[] = new Time[5];
    for (int i = 0; i < 5; i++){
      Time a = new Time("09:00:00");
      for (int j = 1; j < i + 1; j++){   // changed
        a.addHour();
      }
      tstamps[i] = a;
    }

    for (int x = 0; x < 5; x++) {
        System.out.println(tstamps[x].time);
    }

I get the output我得到 output

09:00:00
10:00:00
11:00:00
12:00:00
13:00:00

Here's another approach, using your original code.这是另一种方法,使用您的原始代码。

Time tstamps[] = new Time[5];
Time a = new Time("09:00:00");
for (int i = 0; i < 5; i++){
  tstamps[i] = a;
  a.addHour();
  a = new Time(a.time);
}

Suggestions for improvement(s): Make Time.addHour() return a new Time that is one hour in the future.改进建议:使Time.addHour()返回一个新的Time ,即未来一小时。 That would allow you to simplify the above further.这将允许您进一步简化上述内容。 Like,喜欢,

Time tstamps[] = new Time[5];
Time a = new Time("09:00:00");
for (int i = 0; i < 5; i++){
  tstamps[i] = a;
  a = a.addHour();
}

or或者

Time tstamps[] = new Time[5];
tstamps[0] = new Time("09:00:00");
for (int i = 1; i < tstamps.length; i++) {
  tstamps[i] = tstamps[i-1].addHour();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM