更改链表中对象的3个值中的一个。

huangapple go评论71阅读模式
英文:

Changing one of the 3 values of object in LinkedList

问题

我应该创建一个"clocks"集合,并将它的分钟值增加1。
我已经创建了一个Clock类(以3个整数作为参数),并将它们放入了LinkedList中。
但是,当我尝试获取对象的值时,结果出现错误...
以下是我的想法(是的,我知道如果分钟大于60,我必须添加代码来改变小时):

public static void main(String[] args) throws java.lang.Exception {
    Random randomizer = new Random();

    List<Clock> Clocks = new LinkedList<Clock>();
    for (int i = 0; i < randomizer.nextInt(9) + 1; i++) {
        Clock clock = new Clock(randomizer.nextInt(24), randomizer.nextInt(60), randomizer.nextInt(60));
        Clocks.add(clock);
    }

    for (Clock clock : Clocks) {
        clock.checkTime(); // 这是Clock类中的方法,它会在控制台上打印随机时间,用冒号分隔。
        clock.set(clock.getHour(), clock.getMinute() + 1, clock.getSecond());
    }
}

是否有办法只更改这3个整数中的一个?

我还考虑过创建另一个名为"time"的类,然后将其转换为字符串,Clock类使用此字符串代替3个整数。但是我仍然需要代码从字符串中提取整数并进行更改... 所以我决定不走这条路。

英文:

I'm suppoused to create "clocks" collection and change it's minute Value by 1.
I've created Clock class (taking 3 int as parameters) and put them to LinkedList.
But when I try to get values of object it results with error...
Here is my idea (yes I know i have to add code that will change hour if minute gets bigger than 60):

public static void main (String[] args) throws java.lang.Exception{
Random randomizer = new Random();

List &lt;Clock&gt; Clocks = new LinkedList &lt;Clock&gt;();
for (int i=0; i &lt;randomizer.nextInt(9)+1; i++){
    Clock clock = new Clock(randomizer.nextInt(24)+, randomizer.nextInt(60), randomizer.nextInt(60));
    Clocks.add(clock);
    }
    
    for (Clock clock : Clocks) {
        clock.checkTime();//this method from Clock class just prints on the console randomized time separated with &quot;:&quot;.
        clock.set(clock.getHour(), clock.getMinute()+1, clock.getSecond()); 
 }
}

Is there any way to change just one of these 3 int?

I also considered creating another class "time" that would be then turned to String and the Clock using this String instead of 3 ints. But I stil would need code to extract ints from String and change them... So I decided to not go this way.

答案1

得分: 1

我测试了一下(代码在底部),没有任何错误就可以运行。我在你的代码中唯一需要做的更改是从Clock clock = new Clock(randomizer.nextInt(24)+, randomizer.nextInt(60), randomizer.nextInt(60));中移除了+

关于获取对象值时出现的错误,你能提供更多信息吗?

如果只想改变时钟中的一个数字,你可以在Clock类中添加一个或多个方法来增加这些值(例如,为这3个整数分别添加get/set方法)。

...

public void setH(int h) {
    this.h = h;
}
public void setM(int m) {
    this.m = m;
}
public void setS(int s) {
    this.s = s;
}

...

既然你可能只想递增这些值,你可以在Clock类中像这样做:

...

public void increment(int h, int m, int s) {
    this.h += h;
    this.m += m;
    this.s += s;
}
public void incrementH(){
    this.h++;
}
public void incrementM(){
    this.m++;
}
public void incrementS(){
    this.s++;
}

public void tick() {
    /* this.s++;
    if (this.s >= 60) {
        this.s = 0;
        this.m++;
    } */

    // 只增加分钟
    this.m++;

    if (this.m >= 60) {
        this.m = 0;
        this.h++;
    }
}

...

我对你代码的实现如下:

import java.util.Random;
import java.util.List;
import java.util.LinkedList;

public class HelloWorld {
    public static void main (String[] args) throws java.lang.Exception{
        Random randomizer = new Random();
        
        List<Clock> Clocks = new LinkedList<Clock>();
        for (int i=0; i < randomizer.nextInt(9)+1; i++){
            Clock clock = new Clock(randomizer.nextInt(24), randomizer.nextInt(60), randomizer.nextInt(60));
            Clocks.add(clock);
        }
        
        for (Clock clock : Clocks) {
            clock.checkTime();//this method from Clock class just prints on the console randomized time separated with ":".
            clock.set(clock.getHour(), clock.getMinute()+1, clock.getSecond()); 
        }
    }
}


class Clock {
    private int h, m, s;
    
    public Clock(int h, int m, int s) {
        set(h, m, s);
    }
    
    public void checkTime() {
        System.out.println(h + " " + m + " " + s);   
    }
    public void set(int h, int m, int s) {
        this.h = h;
        this.m = m;
        this.s = s;
    }
    
    public int getHour() {
        return h;
    }
    public int getMinute() {
        return m;
    }
    public int getSecond() {
        return s;
    }
    
    
}
英文:

I tested this out (code at the bottom) and it works without any errors. The only change I had to make to your code was removing the + from Clock clock = new Clock(randomizer.nextInt(24)+, randomizer.nextInt(60), randomizer.nextInt(60));

Could you provide more information on the error getting the values of the objects?

As to changing just one of the numbers on the clock, you could add one or more methods to the Clock class to increment the values. (e.g. get/set for each of the 3 ints)

...

public void setH(int h) {
    this.h = h;
}
public void setM(int m) {
    this.m = m;
}
public void setS(int s) {
    this.s = s;
}

...

Since you probably just want to increment the values, you can do something like this in the Clock class.

...

public void increment(int h, int m, int s) {
    this.h += h;
    this.m += m;
    this.s += s;
}
public void incrementH(){
    this.h++;
}
public void incrementM(){
    this.m++;
}
public void incrementS(){
    this.s++;
}

public void tick() {
    /* this.s++;
    if (this.s &gt;= 60) {
        this.s = 0;
        this.m++;
    } */

    // just the minutes
    this.m++;

    if (this.m &gt;= 60) {
        this.m = 0;
        this.h++;
    }
}

...

My implementation of your code

import java.util.Random;
import java.util.List;
import java.util.LinkedList;

public class HelloWorld {
    public static void main (String[] args) throws java.lang.Exception{
        Random randomizer = new Random();
        
        List &lt;Clock&gt; Clocks = new LinkedList &lt;Clock&gt;();
        for (int i=0; i &lt;randomizer.nextInt(9)+1; i++){
            Clock clock = new Clock(randomizer.nextInt(24), randomizer.nextInt(60), randomizer.nextInt(60));
            Clocks.add(clock);
        }
        
        for (Clock clock : Clocks) {
            clock.checkTime();//this method from Clock class just prints on the console randomized time separated with &quot;:&quot;.
            clock.set(clock.getHour(), clock.getMinute()+1, clock.getSecond()); 
        }
    }
}


class Clock {
    private int h, m, s;
    
    public Clock(int h, int m, int s) {
        set(h, m, s);
    }
    
    public void checkTime() {
        System.out.println(h + &quot; &quot; + m + &quot; &quot; + s);   
    }
    public void set(int h, int m, int s) {
        this.h = h;
        this.m = m;
        this.s = s;
    }
    
    public int getHour() {
        return h;
    }
    public int getMinute() {
        return m;
    }
    public int getSecond() {
        return s;
    }
    
    
}

huangapple
  • 本文由 发表于 2020年10月11日 01:31:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64296336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定