有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java更改LinkedList中object的3个值之一

我被支持创建“时钟”集合,并将其分钟值更改为1。 我已经创建了时钟类(以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个整数中的一个

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


共 (1) 个答案

  1. # 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;
        }
        
        
    }