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;
}
}
答案 0 :(得分:2)
inchesAndFeetToCentimeteres
应为feetAndInchesToCentimeters
,应使用MetricConverter
的实例调用,因为该方法是在Demo
centimeters = converter.feetAndInchesToCentimeters(inputFeet, inputInches);