我无法调用我的方法并将它们重定向到所需的输出。
期望的最终输出
重量转换
1。磅到公斤
2。千克到磅
请选择您要制作的转化类型: 1
请输入磅数:50
50 磅是 22.6796 千克。
(这是部分代码,但是如果有人可以帮助我弄清楚这个问题,我将能够做其余的事情)。
基本上,在'期望的最终输出'声明中,它说: xx磅是xx.xxxx千克我在调用我的其他方法在第57行进行最终计算时遇到了麻烦。 / p>
/**
* @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
答案 0 :(得分: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
答案 1 :(得分: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.");