【问题标题】:How to return Time using a 2-D array (Java)?如何使用二维数组(Java)返回时间?
【发布时间】:2013-07-31 18:19:44
【问题描述】:

编辑:

很遗憾,我不知道如何实现 return 语句。问题出在哪里(MPFP Book 2005)

这个程序旨在增加时间。例如。将 1 小时 40 分钟添加到 2:20 ~ 使用传统的二维数组存储新时间得到 4:00。

我的问题是我不知道如何将整数 'newminutes' 和 'newhours' 附加到二维数组以及以标准时间形式正确返回。

public class AddingTime {

    private static int [] [] nativeClockAdd (int oldHours,int oldMinutes,int addHours,int addMinutes)
    {
        int newMinutes = 0, newHours = 0;

        int[][] time = new int [newMinutes] [newHours]; // return a time.   


    newMinutes = oldMinutes + addMinutes;

    while (newMinutes > 60) // subtract 60 minutes, add an hour
    {
    newMinutes -= 60;
    addHours += 1;
    }
    newHours = oldHours + addHours;

    while (newHours > 12)
    {
    newHours -= 12; 
    }   
    return time;
    }

    public static void main(String[] args) {
        System.out.println (nativeClockAdd(4,5,7,8));

    }
}

【问题讨论】:

  • 这个结构是在某种赋值中定义的还是可以改变的?
  • @Gamb 我不确定如何遵循伪代码 - 这是我关心的问题。现在我应该使用当前编辑重新发布并给贾斯汀最佳答案还是等待有关该问题的帮助?
  • 如果 Justin 帮助您解决了问题,请接受并完成作业。如果没有,请编辑并等待。
  • 你可以用任何你喜欢的方式解决编程难题。您不必逐行翻译此伪代码。阅读伪代码以了解您应该做什么,然后利用您的能力以有效、明智的方式执行操作。这里的伪代码其实非常很糟糕,使用循环做减法和加法是荒谬的。只需使用乘法和除法。本书的下一页是为了让这段代码变得更好的练习吗?
  • @BDillan 我迟到了,抱歉,但您应该按照之前的方式遵循伪代码,如果下一部分使用模数运算符,请按照 Justin 正确指出的方式进行操作。我相信这种作业旨在为您提供一个起始代码,然后向您展示如何调整它以学习解决问题的多种方法。最终,你总会想出一个个人赢家,这将你定义为一名程序员。

标签: java arrays modulo


【解决方案1】:
while (newMinutes > 60) // subtract 60 minutes, add an hour
{
    newMinutes -= 60;
    addHours += 1;
}
newHours = oldHours + addHours;

while (newHours > 12)
{
    newHours -= 12; 
}

这一点都不好。阅读模数运算符。所有这些循环都可以在几行代码中完成。

newMinutes = oldMinutes + addMinutes;           //add minutes
newHours = oldHours + addHours + newMinutes/60; //int truncation is ok, 61/60 = 1
newHours = newHours % 12; //convert to 12hr format
if(newHours == 0) { //12 % 12 is 0, set it back to 12 if that's the case
    newHours = 12;
}

如果您想更轻松地做到这一点,请为 Time 创建一个类,它只是一对表示小时和分钟的整数。你可以返回这个对象而不是一个奇怪的 int[2]。

class MyTime {
    private int minutes;
    private int hours;
    //Make this public if you think you'll need it.
    private MyTime(int h, int m) {
       this.hours = h;
       this.minutes = m;
    }

    public int getMinutes() { return minutes; }
    public int getHours() { return hours; }

    public static MyTime AddTime(int h, int m, int elapsedHours, int elapsedMinutes) {
        m += elapsedMinutes;         //add minutes
        h += elapsedHours + (m/60);  //add hours + the possible hour from minutes. integer truncation here is good. 61/60 = 1
        m = m%60;                    //get rid of excess minutes. 60 becomes 0, 61 becomes 1, etc...
        return new MyTime(h, m);
    }

    //I'm not sure if this will compile.
    /*
    public static int[2] AddTimeWeird(int h, int m, int elapsedHours, int elapsedMinutes) {
        MyTime myTime = AddTime(h, m, elapsedHours, elapsedMinutes);
        int time[] = new int[2];
        time[0] = myTime.getHours();
        time[1] = myTime.getMinutes();
        return time;
    }
    */
}

【讨论】:

    【解决方案2】:

    创建一个代表时间的类,并存储一个数组而不是原始数组。我会在这个时间类中使用双精度,并将其存储在内部的某个预定义单元中。然后,创建以其他单位返回它的方法,如下所示:

    public class Time{
        private double minutes;
        public double getHours(){
            return this.minutes/60;
        }
        public double getMinutes(){
            return this.minutes;
        }
        public void add(Time time){
            this.minutes+=time.minutes;
        }
        //etc
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-06
      • 1970-01-01
      • 2011-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 2022-01-18
      • 1970-01-01
      相关资源
      最近更新 更多