简体   繁体   English

一个返回double的int方法

[英]An int method which returns a double

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 . 我看到返回类型为int ,参数变量也必须是int ,返回变量也必须是integer But my homework states that it must be an int method that returns a double . 但我的作业表明它必须是一个返回doubleint方法。

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. 非常感谢。

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. 没有什么神秘的,参数类型和返回类型彼此没有关系。

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 convertToMiles instead of ConvertToMiles . 另请注意我如何将您的方法更改为convertToMiles而不是ConvertToMiles This is just a java convention which helps make your code easier to read, especially when it grows in size. 这只是一个java约定,它有助于使代码更容易阅读,特别是当它的大小增加时。

Hope this helps, and happy coding :) 希望这有帮助,快乐编码:)

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; 
}
}

要从“...但我的家庭作业声明它必须是一个返回双...的int方法”转换成Java,教师意味着:

 double methodName(int i)

The int method means, the return type of method will be int. int方法的意思是,方法的返回类型为int。 The parameters can be any others. 参数可以是任何其他参数。

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. 正如你所写: public static int ConvertToMiles(int feet, double miles)第一个参数是输入,第二个参数是double,输出是double。 The return value only shows if the operation was succesfully or not. 返回值仅显示操作是否成功。 Only the inside of function needs to fill correctly. 只有函数内部需要正确填充。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java方法返回double | 整型 - Java method returns double | int 如何在Java中打印以int为参数并返回double值的静态方法 - How to print a static method in java that takes an int as parameter and returns a double 返回带有int的char的方法 - method that returns char with int 如何从返回双精度数组的方法中获取所有双精度值? - How to get all double values from a method which returns a double array? 在名为OddTest的类中编写一个名为isOdd()的方法,该方法将int作为参数,如果int为奇数,则返回true - Write a method called isOdd() in a class called OddTest, which takes an int as an argument and returns true if the int is odd 创建一个返回方法,它接受一个 int 数组并在 java 中返回有序和非重复的 int 数组 - Create a Return Method which accepts an int array and returns ordered and a non-duplicated int array in java 在递归方法中将 int 和 double 相乘 - multiplying int and double in recursive method 创建一个成员函数/ method /,该函数使原始数据类型的数组-int,double,String同时 - Creating a member function /method/, which makes array of primitive data types - int, double, String simultaneously 如何解决 main 类型中的方法 add(double, double, int, double) 不适用于参数 (double, double, double, double) - how to solve The method add(double, double, int, double) in the type main is not applicable for the arguments (double, double, double, double) 不带参数并返回双精度的方法 - method that takes no arguments and returns double
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM