[英]why am i not able to get the return value of -1 when the condition is not met?
why am i not able to get the return value of -1
when the condition is not met?为什么不满足条件时我无法获得-1
的返回值?
package calcfeetandinchestocm;
public class Calcfeetandinchestocm {
public static void main(String[] args) {
calcFeetAndInchesToCentimeters(4, 12);
calcFeetAndInchesToCentimeters(-0.5);
}
public static int calcFeetAndInchesToCentimeters(double feet, double inches) {
if (feet >= 0) {
System.out.println("feet are " + feet + \
" and its equal centimeters are " + feet * 12);
}
if (inches >= 0 && inches <= 12) {
System.out.println("inches are " + inches + \
"and its equal centimeters are " + inches * 2.45);
}
return -1;
}
public static int calcFeetAndInchesToCentimeters(double inches) {
if (inches >= 0) {
System.out.println("inches are " + inches + \
"and its equal centimeters are " + inches * 2.45);
}
return -1;
}
}
I'll address your question first.我先回答你的问题。
why am i not able to get the return value of -1 when the condition is not met?为什么不满足条件时我无法获得-1的返回值?
You do not assign the result of the method call to a variable, so it's unclear how you recognize that you're not getting -1.您没有将方法调用的结果分配给变量,因此不清楚您如何识别未获得 -1。 In fact your methods as you have written them will always return -1 since that is the only return statement you have within the method body.事实上,您编写的方法将始终返回 -1,因为这是您在方法主体中唯一的 return 语句。
If you were to assign the result to a variable like this:如果您要将结果分配给这样的变量:
int centimeters = calcFeetAndInchesToCentimeters(4, 12);
System.out.println("4 feet 12 inches converted to centimeters is: "+centimeters);
The call to System.out.println
above would output 4 feet 12 inches converted to centimeters is: -1
上面对System.out.println
的调用将输出4 feet 12 inches converted to centimeters is: -1
There are a few other problems with your code:您的代码还有其他一些问题:
To address #1 - one foot is 30.48cm so obviously this line is incorrect:为了解决 #1 - 一只脚是 30.48 厘米,所以显然这条线是不正确的:
System.out.println("feet are " + feet + \
" and its equal centimeters are " + feet * 12);
Furthermore one inch is 2. 5 4cm so your other calculation is also incorrect:此外一英寸是 2. 5 4cm 所以你的其他计算也是不正确的:
System.out.println("inches are " + inches + \
"and its equal centimeters are " + inches * 2.45);
I think it's probably obvious after what I said in the beginning, but you're also not returning the result of your calculation which is what I believe you intend to do.我认为在我开始说的之后这可能很明显,但是你也没有返回你的计算结果,我相信你打算这样做。 You need to convert feet to centimeters and inches to centimeters then return the sum of those two results.您需要将英尺转换为厘米,将英寸转换为厘米,然后返回这两个结果的总和。
To address #2 - your second method should probably not have the same name as it does not take a feet
parameter at all.为了解决 #2 - 你的第二个方法可能不应该有相同的名称,因为它根本不接受一个feet
参数。
Something like this would be more accurate:这样的事情会更准确:
public static int calcInchesToCentimeters(double inches)
Inside of this method you should not repeat the logic you have already written earlier in the code, instead just call your first method with 0 as the number of feet and pass the number of inches.在此方法中,您不应重复之前已在代码中编写的逻辑,而只需使用 0 作为英尺数并传递英寸数来调用您的第一个方法。
public static int calcInchesToCentimeters(double inches){
return calcFeetAndInchesToCentimeters(0, inches);
}
Lastly some further reading I would suggest for you:最后,我建议您进一步阅读:
Java Tutorial: Returning a Value from a Method Java 教程:从方法返回值
You have to set a variable equal to the function or do a system out print with the function as it's argument like:您必须设置一个等于该函数的变量,或者使用该函数作为参数进行系统输出,例如:
System.out.print(calcFeetAndInchesToCentimeters(4, 12));
or或者
x = calcFeetAndInchesToCentimeters(4, 12);
System.out.print(x);
Here in your code, it dose not depend on condition so either your condition is true or false it returns always -1 so, you just need to grab return value from function.在您的代码中,它不依赖于条件,因此无论您的条件是真还是假,它始终返回 -1,因此,您只需要从函数中获取返回值。 Like,喜欢,
int result = calcFeetAndInchesToCentimeters(4, 12);
System.out.println("Result is " + result);
it surely print -1.它肯定会打印-1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.