我是一名正在学习Java的新手程序员,虽然我的作业已经完成但我想让它更整洁。我想知道我是否可以将以下打印方法合并为一个并且至少将千克与磅和磅公斤方法合并为一个。
注意,如果语句和循环,我不能使用更高级的东西。
因为这是我第一次发帖,我想确保为所有回答者提供足够的信息,我已将我的体重转换java文件上传到这里:Weight Conversion Java file。
关于如何简化代码或遵循更好的代码礼仪的任何其他建议也受到欢迎。
以下是印刷声明:
/**
* This method below prints the calculations calculateKG and calculateLBS
*/
public static void printRESULTS1( double dResult1, double dResult2){
// Prints the result of Pounds to Kilograms
System.out.print(dResult1 + " pounds is " + dResult2 + " kilograms.");
}// end method printRESULTS1
/**
* This method below prints the calculations calculateKG and calculateLBS
*/
public static void printRESULTS2( double dResult1, double dResult2){
// Prints the result of Pounds to Kilograms
System.out.print( dResult1 + " kilograms is " + dResult2 + " pounds");
}// end method printRESULTS2
/**
* This method below prints the calculations calculateOZ and calculateLBS
*/
public static void printRESULTS3( double dResultOZ, double dResultLBS){
// Prints the result of Pounds to Kilograms
System.out.print( dResultOZ + " ounces is " + dResultLBS + " pounds");
}// end method printRESULTS3
/**
* This method below prints the calculations calculateOZ and calculateLBS
*/
public static void printRESULTS4( double dResultLBS, double dResultOZ){
// Prints the result of Pounds to Kilograms
System.out.print( dResultLBS + " pounds is " + dResultOZ + " ounces ");
}// end method printRESULTS4
答案 0 :(得分:1)
首先,请考虑一下:
public static void printResults(
double dResultFrom,
String from,
double dResultTo,
String to)
{
System.out.print(dResultFrom + " " + from + " is " + dResultTo + " " + to);
}
不确定您使用它的整个环境以及您的局限性。当然,进一步的重构步骤是可能的。例如:
public static void printResults(
double resultFrom,
String fromDescription,
double resultTo,
String toDescription)
{
String formattedResult = formatResult(
resultFrom,
fromDescription,
resultTo,
toDescription);
System.out.print(formattedResult);
}
public static String formatResult(
double resultFrom,
String fromDescription,
double resultTo,
String toDescription)
{
return formatQuantity(resultFrom, fromDescription)
+ " is "
+ formatQuantity(resultTo, toDescription);
}
public static String formatQuantity(double value, String description)
{
return value + " " + description;
}
请注意比示例中的代码重复少得多,并明确分离职责(格式化功能和打印功能)。例如,如果您必须将结果打印到文件而不是控制台,则此设计将更加灵活。
答案 1 :(得分:0)
感谢您的帮助,这让我更加思考如何简化代码并更好地组织代码。关于确保代码可以像print方法一样通用的好建议也可以用于另一种非英语的语言。真正有用的是理解我在一个方法中可以有超过2个参数。
public static void printRESULTS(int nConversion, double dResult1, double dResult2){
//Declare variable
String output = "";
//Pounds to Kilogram output
if (nConversion == 1){
output = dResult1 + " pounds is " + dResult2 + " kilograms.";
System.out.println(output);
}
//Kilograms to Pounds output
else if (nConversion == 2){
output = dResult1 + " kilograms is " + dResult2 + " pounds. ";
System.out.println(output);
}
//Ounces to Pounds output
else if (nConversion == 3){
output = dResult1 + " ounces is " + dResult2 + " pounds. ";
System.out.println(output);
}
//Pounds to Ounces output
else if (nConversion == 4){
output = dResult1 + " pounds is " + dResult2 + " ounces. ";
System.out.println(output);
}