【问题标题】:Getting "The method inchesAndFeetToCentimeteres(double, double) is undefined for the type Demo" [closed]获取“Demo类型的方法inchesAndFeetToCentimeteres(double,double)未定义”[关闭]
【发布时间】:2014-10-05 19:36:20
【问题描述】:

Demo.Java:

import javax.swing.*;

/** 
 * This class is used to demonstrate the functionality of the MetricConverter
 * class.
 */    
class Demo {

    public static void main (String[] args) {

        MetricConverter converter = new MetricConverter();

        double inputInches;
        double inputFeet;
        double centimeters, inches;
        String inputInchesAsString;
        String inputFeetAsString;
        //Get input
        inputInchesAsString = JOptionPane.showInputDialog(null, "Enter inches: ");
        inputInches =  Double.parseDouble(inputInchesAsString);

        inputFeetAsString = JOptionPane.showInputDialog(null, "Enter feet: ");
        inputFeet = Double.parseDouble(inputFeetAsString);
        //Perform various conversion routines

        centimeters = converter.inchesToCentimeters(inputInches);
        inches = converter.centimetersToInches(centimeters);

下面的行是我无法弄清楚的问题。我的方法创建正确并返回正确的类型,但它仍然不起作用。

        centimeters = inchesAndFeetToCentimeteres(inputFeet, inputInches);

        //Display the result
        JOptionPane.showMessageDialog(null, "Input: " + inputInches + 
                                      " inches is equivalent to " +
                                      centimeters + " centimeters");
        JOptionPane.showMessageDialog(null, "Converting back to inches: " + inches);

        JOptionPane.showMessageDialog(null, "Input: " + inputFeet +" Feet and " + inputInches +  
                                      " inches is equivalent to " +
                                      centimeters + " centimeters");
        JOptionPane.showMessageDialog(null, "Converting back to inches: " + inches);
    }


}

MetricConverter.java:

/**
 * This class provides various routines to
 * convert metric measurements to U.S. units and
 * vice versa.
 */
class MetricConverter {

    //----------------------------------
    //    Data Members
    //----------------------------------

    /**
     * A factor to convert inches to centimeters
     */
    public static final double INCHES_TO_CENTIMETERS = 2.54; 

    /**
     * A factor to convert centimeters to inches
     */
    public static final double CENTIMETERS_TO_INCHES = 1 / 2.54; 

    /**
     * A factor to convert feet to inches
     */
    public static final double FEET_TO_INCHES = 12.0;

    //----------------------------------
    //    Constructors
    //----------------------------------

    /**
     * Default constructor
     */
    public MetricConverter() {

    }

    //-------------------------------------------------
    //  Public Methods:
    // 
    //      double  inchesToCentimeters        ( double         )
    //      double  centimetersToInches        ( double         )
    //      double  feetAndInchesToCentimeters ( double, double )
    //------------------------------------------------

    /**
     * Converts a given length in inches to
     * equivalent centimeters.
     * 
     * @param inches  the length expressed in inches
     * @return length expressed in centimeters
     */
    public double inchesToCentimeters(double inches) {
        return inches * INCHES_TO_CENTIMETERS;
    }

    /**
     * Converts a given length in centimeters to
     * equivalent inches.
     * 
     * @param centimeters the length expressed in centimeters
     * @return length expressed in inches
     */
    public double centimetersToInches(double inches) {
        return inches * CENTIMETERS_TO_INCHES;
    }

    /**
     * Converts a given length in feet and inches to
     * equivalent centimeters.
     * 
     * @param feet   the feet portion of the length
     * @param inches the inch portion of the length
     * @return length expressed in centimeters
     */
    public double feetAndInchesToCentimeters(double inputFeet, double inputInches) {
        inputInches = (inputFeet * FEET_TO_INCHES) + inputInches;
        double centimeters = inchesToCentimeters(inputInches);
        return centimeters;
    }                
}

【问题讨论】:

    标签: java eclipse swing


    【解决方案1】:

    inchesAndFeetToCentimeteres 应该是 feetAndInchesToCentimeters 并且应该使用 MetricConverter 的实例来调用,因为该方法是在那里而不是在 Demo 中定义的

    centimeters = converter.feetAndInchesToCentimeters(inputFeet, inputInches);
    

    【讨论】:

      猜你喜欢
      • 2015-12-14
      • 1970-01-01
      • 1970-01-01
      • 2021-01-29
      • 2016-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多