简体   繁体   English

为什么不满足条件时我无法获得-1的返回值?

[英]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:您的代码还有其他一些问题:

  1. You are not correctly calculating the result of the conversion.您没有正确计算转换结果。
  2. You have a second method with the same name as the first but with fewer parameters and you're repeating part of the logic in the first method.您有第二种方法,其名称与第一种方法相同,但参数较少,并且您在第一种方法中重复了部分逻辑。

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 教程:从方法返回值

The DRY Principle干燥原则

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.

相关问题 为什么我无法退回片段? - Why am I not able to return a fragment? 为什么我无法获得javac -version? - Why am I not able to get javac -version? 为什么我无法在输出中返回数组的索引? - Why am I not able to return the index of the array in the output? 进入页面后为什么无法按按钮? - Why am I not able to press the buttons when I come into the page? 为什么我无法在 for 循环之外打印值 - Why am I not able to print the value outside th e for loop 为什么我不能在此方法中更改布尔变量的值 - Why am I not able to change the value of the boolean variable in this method 为什么我无法在Insertionsort Coded Java中获得输出 - Why i am not able to get output in Insertionsort Coded java 给我时为什么会得到不兼容的类型? 在类中扩展并返回 - Why do I get incompatible types when I am giving ? extends in both class and return 我无法将程序中找到的 boolean 结果的正确值分配给 boolean 变量并使用结果检查条件 - I am Not able to assign the correct value of boolean result found in the program , to a boolean variable and to check a condition with the result 使用if-else并且满足所有可能的条件时,为什么在方法中需要附加的return语句 - Why do i need an additional return statement in a method when using if-else and all possible conditions are met
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM