0

我无法就地调用我的方法并将它们重定向到所需的输出。

期望的最终输出

重量换算

1.磅到公斤

2.公斤到磅

请选择您要进行的转换类型:1

请输入英镑:50

50磅是22.6796公斤。

(这是部分代码,但如果有人可以帮助我解决这个问题,我将能够完成剩下的工作)。

基本上,在“期望的最终输出”语句中,它说:xx 磅是 xx.xxxx 公斤,我无法调用我的其他方法在第 57 行进行最终计算。

 /**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    //Declare variables
    double nInitialPounds = 0;
    double nInitialKilos = 0.0;
    int nWeightSelection = 0;
    double dInitialWeight = 0.0;
    double dFinalWeight = 0.0;
    double nPoundsToKilos = 0.0;
    double nKilosToPounds = 0.0;
    Scanner input = new Scanner (System.in);

    //Display program title
    System.out.println("WEIGHT CONVERSION");

    //Display program option selections
    System.out.println("1. Pounds to kilograms");

    System.out.println("2. Kilograms to pounds");

    //Print blank line to console
    System.out.println("");

    //Prompt user to make selection of desired output
    System.out.println("Please select the type of conversion you would like to make: ");

    //Read selection by user
    nWeightSelection = input.nextInt();


   //Begin while-loop statement
while (nWeightSelection == 1) {

    //Prompt user to enter the amount of weight to be converted
    System.out.print("Please enter the pounds: ");

    // Read number entered by the user
    dInitialWeight = input.nextDouble();

    //Call weight conversion for Pounds to Kilos
    nPoundsToKilos = poundsToKilos(nInitialKilos, dInitialWeight);

    //Print final weight
    System.out.println(+dInitialWeight + " pounds is " + nPoundsToKilos + " kilograms.");

    break;
}

} //end Main method

/**
 * This method converts weight in pounds to kilograms
 * @param nPounds       beginning weight in pounds
 * @param nKilos        desired output into kilograms
 * @return nFinalWeight     final weight in desired output
 */
public static double poundsToKilos(double nKilos, double nPounds) {
    //Declare variables
    double dFinalWeight = 0.0;                    //Final weight conversion from pounds to kilos

    //Calculate weight conversion
    dFinalWeight = (nPounds*2.204);

    return dFinalWeight;

} //end method poundsToKilos

/**
 * This method prints the converted weight to the console
 * @param nPounds       beginning weight in pounds
 * @param nKilos        desired output into kilograms
 * @return nFinalWeight     final weight in desired output
 */
public static double printWeight(double nKilos, double nPounds) {
    //Declare variables
    double dFinalWeight = 0.0;                    //Final weight conversion

    return dFinalWeight;

} //end method printWeight
4

2 回答 2

1

试试这个代码:

 /**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    //Declare variables
    double nInitialPounds = 0;
    double nInitialKilos = 0.0;
    int nWeightSelection = 0;
    double dInitialWeight = 0.0;
    double dFinalWeight = 0.0;
    double nPoundsToKilos = 0.0;
    double nKilosToPounds = 0.0;
    Scanner input = new Scanner(System.in);

    //Display program title
    System.out.println("WEIGHT CONVERSION");

    //Display program option selections
    System.out.println("1. Pounds to kilograms");

    System.out.println("2. Kilograms to pounds");

    //Print blank line to console
    System.out.println("");

    //Prompt user to make selection of desired output
    System.out.println("Please select the type of conversion you would like to make: ");

    //Read selection by user
    nWeightSelection = input.nextInt();


    //Begin while-loop statement
    while (nWeightSelection == 1) {
        //Prompt user to enter the amount of weight to be converted
        System.out.print("Please enter the pounds: ");

        // Read number entered by the user
        dInitialWeight = input.nextDouble();

        //Call weight conversion for Pounds to Kilos
        nPoundsToKilos = poundsToKilos(nInitialKilos, dInitialWeight);

        //Print final weight
        System.out.println(+dInitialWeight + " pounds is " + nPoundsToKilos + " kilograms.");

        break;
    }

    //Call weight conversion for Pounds to Kilos
    //nPoundsToKilos = poundsToKilos(nInitialKilos, nInitialPounds);

    //Call weight conversion for Kilos to Pounds
    nKilosToPounds = kilosToPounds(nInitialPounds, nInitialKilos);

} //end Main method

/**
 * This method converts weight in pounds to kilograms
 *
 * @param nPounds beginning weight in pounds
 * @param nKilos desired output into kilograms
 * @return nFinalWeight final weight in desired output
 */
public static double poundsToKilos(double nKilos, double nPounds) {
    //Declare variables
    double dFinalWeight = 0.0;                    //Final weight conversion from pounds to kilos

    //Calculate weight conversion
    dFinalWeight = nPounds * 2.204;

    return dFinalWeight;

} //end method poundsToKilos

/**
 * This method prints the converted weight to the console
 *
 * @param nPounds beginning weight in pounds
 * @param nKilos desired output into kilograms
 * @return nFinalWeight final weight in desired output
 */
public static double printWeight(double nKilos, double nPounds) {
    //Declare variables
    double dFinalWeight = 0.0;                    //Final weight conversion

    return dFinalWeight;

} //end method printWeight
于 2013-10-08T19:19:51.683 回答
1

首先,这段代码中有太多的注释,这使得它非常难以阅读。您有描述性变量名称,重复相同信息的注释是多余的。

在编程中,执行操作的顺序很重要。在这种情况下,您在实际获得之前打印出最终重量。一个好的 IDE 会警告您未使用 nPoundsToKilos。

交换

//Print final weight
System.out.println(dInitialWeight + " pounds is "  + nPoundsToKilos + " kilograms.");

//Call weight conversion for Pounds to Kilos
nPoundsToKilos = poundsToKilos(nInitialKilos, nInitialPounds);

//Call weight conversion for Pounds to Kilos
nPoundsToKilos = poundsToKilos(nInitialKilos, nInitialPounds);

//Print final weight
System.out.println( + dInitialWeight + " pounds is "  + nPoundsToKilos + " kilograms.");
于 2013-10-08T19:22:13.520 回答