Java 返回双精度值的 int 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9713069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:34:25  来源:igfitidea点击:

An int method which returns a double

javamethods

提问by Evolutionary High

I've been trying to search online and look at books for this one but I can't find a solution. Everywhere I look with the return type int, the parameter variables also must be int, and the return variable also must be an integer. But my homework states that it must be an intmethod that returns a double.

我一直在尝试在线搜索并查看有关此书的书籍,但找不到解决方案。在我查看返回类型的任何地方int,参数变量也必须是int,而返回变量也必须是integer. 但是我的作业指出它必须是一个int返回double.

So far here is what I have:

到目前为止,这是我所拥有的:

import java.util.Scanner;

public class ConvertF {

    public static void main(String args[]){

    int n =0;

    Scanner s = new Scanner(System.in);

    System.out.println("How many feet do you wish to convert to miles?");

    n = s.nextInt();

    System.out.println("Passing values to ConverToMiles");

    ConvertToMiles(422, 142);

    System.out.printf(feet + "feet equals %2.2n miles", miles);




}

    public static int ConvertToMiles(int feet, double miles){
        int answer;
        double f = (double)feet;
        answer = feet/5280;
        double a = (double)answer;
        return answer;




    }

}

Please help me out with a hint or a solution. Thanks a lot.

请帮助我提供提示或解决方案。非常感谢。

回答by joshuahealy

ALl you need to do is change

你需要做的就是改变

public static int ConvertToMiles(int feet, double miles){

to

public static double ConvertToMiles(int feet, double miles){

And then change the method so it's not converting to and from ints so much. When you do arithmetic with a double and an int, the return type of the expression is a double, unless you cast it.

然后更改方法,这样它就不会在整数之间进行太多转换。当您使用 double 和 int 进行算术运算时,表达式的返回类型是 double,除非您对其进行强制转换。

Nothing mysterious, parameter types and return types have no relation to each other.

没什么神秘的,参数类型和返回类型彼此没有关系。

回答by Java42

To translate from "...but my homework states that it must be an int method that returns a double..." into Java, the instructor means:

要将“...但我的作业说明它必须是返回双精度值的 int 方法...”翻译成 Java,教师的意思是:

 double methodName(int i)

回答by Aak

The int method means, the return type of method will be int. The parameters can be any others.

int 方法意味着,方法的返回类型将为 int。参数可以是任何其他参数。

As you have written: public static int ConvertToMiles(int feet, double miles)The first parameter will be the input and the second one, with type double will be the output. The return value only shows if the operation was succesfully or not. Only the inside of function needs to fill correctly.

正如您所写:public static int ConvertToMiles(int feet, double miles)第一个参数将是输入,第二个参数是 double 类型的将是输出。返回值仅显示操作是否成功。只有函数内部需要正确填充。

回答by Hayden

Ok so there are quite a few problems in this program. The code should look something along the lines of:

好的,所以这个程序有很多问题。代码应该看起来像:

import java.util.Scanner;

public class ConvertF {

    public static void main(String args[]){

        Scanner s = new Scanner(System.in);

        System.out.println("How many feet do you wish to convert to miles?");

        //Grab the user's input and store in feet input
        int feetInput = s.nextInt();

        System.out.println("Passing values to ConverToMiles");

        //Store the value returned by convertToMiles in milesOutput
        double milesOutput = convertToMiles(feetInput);

        //This prints the output - note how I have used "%2.2f" as we are now working
        //with floats (or more precisely doubles)
        System.out.printf(feetInput + " feet equals %2.2f miles", milesOutput);
    }

    /*
     * The word after static is the type the method returns (in this case a double)
     * the parameter (int feet) has local scope to this method. This means that 
     * only this method can see the variable 'feet' - basically you cannot use feet 
     * in the main method above. You are not required to declare a 'miles' variable as
     * this is the value the method is returning, it can be stored in a variable where
     * the method is called
     */
    public static double convertToMiles(int feet){
        return feet/5280.0; //One of these values must be a double to return a double
    }

}

Please read the comments to gain a better insight into how things are working.

请阅读评论以更好地了解事情的运作方式。

Also note how I have changed your method to convertToMilesinstead of ConvertToMiles. This is just a java convention which helps make your code easier to read, especially when it grows in size.

另请注意我如何将您的方法更改为convertToMiles而不是ConvertToMiles. 这只是一个 Java 约定,它有助于使您的代码更易于阅读,尤其是当它变大时。

Hope this helps, and happy coding :)

希望这会有所帮助,并祝您编码愉快:)

回答by Tushar Paliwal

One solution of your question may be:

您的问题的一种解决方案可能是:

class Three
{
public static void main(String arg[])
{
int b=new Three().one();
System.out.println(b);
}
public  int one() 
{
double a=10;
return (int)a; 
}
}