gpt4 book ai didi

java - 将其他方法的参数调用回主方法以进行最终计算和输出

转载 作者:行者123 更新时间:2023-12-02 06:39:52 25 4
gpt4 key购买 nike

我在适当调用我的方法并将它们重定向到所需的输出时遇到问题。

期望的最终输出

重量换算

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

最佳答案

试试这个代码:

 /**
* @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

关于java - 将其他方法的参数调用回主方法以进行最终计算和输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19256234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com