【问题标题】:What is the correct return statement other than return secondpass?除了 return secondpass 之外,正确的 return 语句是什么?
【发布时间】:2020-01-10 04:34:00
【问题描述】:

除了return secondpass,正确的返回语句是什么?

为什么return secondpass 仍然可以在IDE IntelliJ 中没有错误显示并且能够运行?

 public static void main(String[] args) {
    getDurationString(5,2);
}

public static int getDurationString(int minutes, int seconds) {
    if (minutes < 0 || seconds < 0 && seconds > 59) {
        System.out.println("Invalid value");
    }
    int hourtomin = (60/minutes);
    int secondspass = (seconds/seconds);
    System.out.println(hourtomin + "hours" + secondspass + "seconds");
    return secondspass;
}

【问题讨论】:

  • 60 / 512 ... 很确定这不是您所期望的。我的建议是从一张纸和笔开始,试着找出你“似乎”在尝试的数学。还要问问自己,如果方法名为getDurationString,你希望它返回什么类型的值
  • 很高兴知道您遇到的具体错误:具体来说,是编译器错误还是运行时异常,以及具体错误。您能否请edit这个问题提供有关该错误的更多详细信息?
  • 另外,还有一个改进建议:不要依赖 if 块中的运算符优先级:使用括号使您的代码对其他人更清晰。最后,能否请edit 详细说明getDurationString 方法应该计算和返回的内容?
  • seconds &lt; 0 &amp;&amp; seconds &gt; 59 永远不会是真的。

标签: java return


【解决方案1】:

返回的最合乎逻辑的东西是来自名为 getDurationString 的方法的持续时间字符串。所以我会将返回类型更改为字符串(正如 MadProgrammer 所暗示的那样)。此外,由于该方法没有表明它将打印某些内容,因此样式会说让调用者处理输出。最后,为了回答另一个问题,IDE 很乐意让您从返回类型为 int 的方法返回一个 int (secondspass),即使它不是您想要的或计算不正确。

我调整了计算和检查以首先将秒转换为分钟(以防秒导致分钟超过一个小时 - 我这样做是因为我想让秒 > 59),然后打破分钟减少到小时和秒(加上原来的秒)。代码如下(我还尝试了驼峰式变量;也请注意,由于传递的变量是原始类型,我可以安全地修改它们而不会对调用者产生副作用)。

public class TimeString {

public static String getDurationString(int minutes, int seconds) {
    //since this method is called get, having a side-effect like printing something is undesirable
    if (minutes < 0 || seconds < 0) { //enhanced to handle seconds > 59
        return "Invalid value";
    }
    //handle any seconds that could be minutes
    minutes += seconds / 60;
    seconds %= 60;
    int hours = (minutes / 60);
    minutes %= 60;
    seconds = (seconds + 60 * minutes);
    return hours + " hours " + seconds + " seconds"; //note spaces so things look nice, you asked for hours and seconds, hours, minutes, and seconds is more usual
}

public static void main(String args[]) {
    System.out.println(getDurationString(105, 900));
}
}

其他想法: 在一段时间内使用小时、秒和无分钟似乎非常不合常规。如果将分钟保留为分钟而不是将其转换为秒,则代码如下所示。

public class TimeString {

public static String getDurationString(int minutes, int seconds) {
    //since this method is called get, having a side-effect like printing something is undesirable
    if (minutes < 0 || seconds < 0) { //enhanced to handle seconds > 59
        return "Invalid value";
    }
    //handle any seconds that could be minutes
    minutes += seconds / 60;
    seconds %= 60; //and mod by 60 to get any remaining seconds
    int hours = (minutes / 60);//handle whether there are full hours from the minutes
    minutes %= 60;// and mod by 60 to get the remaining minutes
    //removed line converting minutes back to seconds
    return hours + " hours " + minutes + " minutes " + seconds + " seconds"; //note spaces so things look nice, you asked for hours and seconds, hours, minutes, and seconds is more usual
}

public static void main(String args[]) {
    System.out.println(getDurationString(95, 79));// should become 1 hours 36 minutes 19 seconds
}
}

【讨论】:

    猜你喜欢
    • 2014-12-22
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    • 2021-11-14
    相关资源
    最近更新 更多