Java 除了return secondpass之外,正确的return语句是什么?

Java 除了return secondpass之外,正确的return语句是什么?,java,return,Java,Return,除了返回secondpass之外,正确的返回语句是什么 为什么可以返回secondpass,但仍然能够在IDEIntelliJ中显示错误,并且能够运行 public static void main(String[] args) { getDurationString(5,2); } public static int getDurationString(int minutes, int seconds) { if (minutes < 0 || seconds <

除了返回secondpass之外,正确的返回语句是什么

为什么可以
返回secondpass
,但仍然能够在IDEIntelliJ中显示错误,并且能够运行

 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;
}
publicstaticvoidmain(字符串[]args){
getDurationString(5,2);
}
公共静态int getDurationString(int分钟,int秒){
如果(分钟<0 | |秒<0和秒>59){
System.out.println(“无效值”);
}
inthourtomin=(60/分钟);
int secondspass=(秒/秒);
系统输出打印项数(小时+小时+秒+秒);
返回第二个SPASS;
}

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

我稍微调整了计算和检查,首先将秒转换为分钟(如果秒导致分钟超过一小时——我这样做是因为我想允许秒>59),然后将分钟分解为小时和秒(加上原始秒)。代码如下(我还尝试使用camel case变量;请注意,由于传递的变量是基元类型,因此我可以安全地修改它们,而不会对调用方产生副作用)

公共类时间字符串{
公共静态字符串getDurationString(整数分钟,整数秒){
//由于此方法称为get,因此产生诸如打印内容之类的副作用是不可取的
如果(分钟<0 | |秒<0){//增强为处理秒数>59
返回“无效值”;
}
//处理可能是分钟的任何秒
分+=秒/60;
秒%=60;
整小时=(分钟/60);
分钟%=60;
秒=(秒+60*分钟);
返回时数+“时数”+秒数+“秒数”;//注意空格,这样事情看起来很好,您要求时数和秒数,时数、分数和秒数更常见
}
公共静态void main(字符串参数[]){
System.out.println(getDurationString(105900));
}
}
其他想法: 在一段时间内使用小时、秒和分钟似乎很不传统。如果将分钟保持为分钟,而不是将其转换为秒,那么代码就是这样的

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
}
}
公共类时间字符串{
公共静态字符串getDurationString(整数分钟,整数秒){
//由于此方法称为get,因此产生诸如打印内容之类的副作用是不可取的
如果(分钟<0 | |秒<0){//增强为处理秒数>59
返回“无效值”;
}
//处理可能是分钟的任何秒
分+=秒/60;
秒%=60;//并mod 60以获得剩余的秒数
int hours=(minutes/60);//处理分钟中是否有完整的小时
分钟%=60;//并mod 60以获得剩余的分钟数
//删除了将分钟转换回秒的行
返回小时+“小时”+分钟+“分钟”+秒+“秒”;//注意空格使事情看起来很好,您要求输入小时和秒,小时、分钟和秒更常见
}
公共静态void main(字符串参数[]){
System.out.println(getDurationString(95,79));//应该变成1小时36分19秒
}
}

60/5
12
。。。这肯定不是你所期望的。我的建议是从一张纸和一支笔开始,试着找出你“似乎”要尝试的数学。还要问问你自己,如果方法名为
getDurationString
,你希望它返回什么类型的值。最好知道你得到的具体错误:具体来说,是编译器错误还是运行时异常,以及具体错误。你能用更多关于错误的细节来回答这个问题吗?还有一个改进建议:在
if
块中不要依赖运算符优先级:使用括号让别人更清楚地看到你的代码。最后,请详细说明
getDurationString
方法应该计算和返回什么?
seconds<0&&seconds>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; //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
}
}