2015-04-07 79 views
0

我遇到了我的代码输出问题。似乎我缺少我创建的方法中的某些东西...我有指示返回总英寸数。返回后我放置totInches,并得到一个错误,指出totInches不是一个变量。不确定这里缺少什么,因为我只是想创建一个方法。大部分代码都是写入的,我应该创建的唯一部分是第二个convertToInches方法..任何建议?转换高度为英寸(Java)

import java.util.Scanner; 

public class FunctionOverloadToInches { 

    public static double convertToInches(double numFeet) { 
     return numFeet * 12.0; 
    } 

    public static double convertToInches(double numFeet, double numInches) { 
     return totInches * 12.0; 
    } 

    public static void main (String [] args) { 
     double totInches = 0.0; 

     totInches = convertToInches(4.0, 6.0); 
     System.out.println("4.0, 6.0 yields " + totInches); 

     totInches = convertToInches(5.9); 
     System.out.println("5.9 yields " + totInches); 
     return; 
    } 
} 
+0

它可能是但是大部分代码在软件中是不可编辑的,我们的类运行它,所以我只允许编辑给我的东西,那就是“public static double convertToInches(double numFeet,double numInches ){ return totInches * 12.0; } “ –

+0

您的'convertToInches'方法中的变量'totInches'未声明,它在您的方法无法访问的主方法中。换句话说,'convertToInches'中的'totInches'没有范围,或者是'null'可能是 –

+0

首先在你的脑海中解决问题。如何计算英寸,给定一个英尺的数字和另外的英寸的数字?以convertToInches(double numFeet)方法为指导。您没有遇到Java或编程问题,而只是概念化手头的任务。 – MarsAtomic

回答

1

变量totInches是不是在你的功能范围定义:

public static double convertToInches(double numFeet, double numInches) { 
    return totInches * 12.0; 
} 

你可以在这个功能使用的都是您所创建的那些唯一的变量,定义为形参的那些:numFeetnumInches。因此,您必须提出一个公式,该公式需要numFeet并将其转换为英寸,同时考虑到numInches中提供的附加英寸。

+0

我明白了! 'public static double convertToInches(double numFeet,double numInches){ return numFeet * 12 + 6; }' –

+0

到达那里! 'numInches'呢? – egracer

+0

我以为我需要为此做些事情,但我的代码经历了所有的检查。不确定为什么他们让我在我的第二种方法中使用numInches lol –

0

你在你的main方法内声明了双变量“totInches”,但你试图在你的“convertToInches”方法内部访问它。在特定方法中声明变量时,该变量只能由该方法访问。你的“convertToInches”只知道你在参数中传递给它的两个变量:numFeet和numInches。然后它看着你的退货声明,看到“totInches”,并不知道它是什么。

我也弄不明白这是什么要做的?

 public static double convertToInches(double numFeet, double numInches) { 
     return totInches * 12.0; 
    } 

你为什么要传递的双变量numFeet和numInches?该功能没有使用它们。我也不明白你为什么需要脚的数量和如果方法,它的名字,试图将东西转换成英寸,英寸数英寸数

相关问题