3

我是一名正在学习 Java 的新手程序员,虽然我的作业完成了,但我想让它更整洁。我想知道我是否可以将以下打印方法合二为一,至少将公斤到磅和磅到千克方法合二为一。

注意,我不能使用比 if 语句和循环更高级的东西。

因为这是我第一次发帖,我想确保为所有回答者提供足够的信息,所以我已将我的体重转换 Java 文件上传到此处:体重转换 Java 文件

关于如何简化代码或遵循更好的代码礼仪的任何其他建议也受到欢迎。

以下是打印语句:

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

2 回答 2

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;
}

请注意,与您的示例相比,代码重复要少得多,并且职责明确分离(格式化功能和打印功能)。例如,如果您必须将结果打印到文件,而不是控制台,那么这种设计将被证明更加灵活。

于 2013-10-06T22:11:14.157 回答
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);
    }
于 2013-10-07T00:18:17.400 回答