[英]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
. 但我的作业表明它必须是一个返回
double
的int
方法。
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.