【问题标题】:Changing one of the 3 values of object in LinkedList更改 LinkedList 中对象的 3 个值之一
【发布时间】:2020-10-10 17:31:17
【问题描述】:

我应该创建“时钟”集合并将其分钟值更改为 1。 我创建了 Clock 类(以 3 个 int 作为参数)并将它们放入 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();//this method from Clock class just prints on the console randomized time separated with ":".
        clock.set(clock.getHour(), clock.getMinute()+1, clock.getSecond()); 
 }
}

有没有办法只改变这 3 个 int 之一?

我还考虑创建另一个类“时间”,然后使用此字符串而不是 3 个整数将其转换为字符串和时钟。但我仍然需要代码来从 String 中提取整数并更改它们......所以我决定不走这条路。

【问题讨论】:

    标签: java object linked-list


    【解决方案1】:

    我对此进行了测试(底部的代码),它可以正常工作,没有任何错误。我必须对您的代码进行的唯一更改是从 Clock clock = new Clock(randomizer.nextInt(24)+, randomizer.nextInt(60), randomizer.nextInt(60)); 中删除 +

    您能否提供有关获取对象值的错误的更多信息?

    至于只更改时钟上的一个数字,您可以向Clock 类添加一个或多个方法来增加值。 (例如,获取/设置 3 个整数中的每一个)

    ...
    
    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++;
        } */
    
        // just the minutes
        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;
        }
        
        
    }
    

    【讨论】:

    • 谢谢。它起作用了 - 我使用了方法刻度(稍微改变了,因为它应该是 >59 而不是 >60),并且它完美地运行了。完整的代码现在在这个链接中(之前没有勾选方法,但这是唯一的区别):kodilla.com/pl/project-java/175400#
    猜你喜欢
    • 1970-01-01
    • 2015-09-22
    • 2011-01-21
    • 1970-01-01
    • 2020-12-26
    • 2020-11-11
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多