简体   繁体   English

如何删除此代码行中的重复项?

[英]How to remove duplication in this line of code?

How do I simplify this code into the simplest form without duplication? 如何将这些代码简化为最简单的形式而不重复? Just using simple if/else statements as this is my entry class for programming 只使用简单的if / else语句,因为这是我编程的入门课程

if (Character.toUpperCase(choosePassOverPed) == 'Y'){
        passengerPoints = passengerPoints + toughCallThresholdPoints;
        if (passengerPoints > pedestrianPoints){
            System.out.println("Fatality Group: PEDESTRIANS");
            System.out.printf("Number of adult fatalities: %d%n", numAdultPedestrians);
            System.out.printf("Number of child fatalities: %d%n", numChildPedestrians);
        }
        else{
            System.out.println("Fatality Group: OCCUPANTS");
            System.out.printf("Number of adult fatalities: %d%n", numAdultPassengers);
            System.out.printf("Number of child fatalities: %d%n", numChildPassengers);
        }
    }
    else{
        pedestrianPoints = pedestrianPoints + toughCallThresholdPoints;
        if (passengerPoints > pedestrianPoints){
            System.out.println("Fatality Group: PEDESTRIANS");
            System.out.printf("Number of adult fatalities: %d%n", numAdultPedestrians);
            System.out.printf("Number of child fatalities: %d%n", numChildPedestrians);
        }
        else{
            System.out.println("Fatality Group: OCCUPANTS");
            System.out.printf("Number of adult fatalities: %d%n", numAdultPassengers);
            System.out.printf("Number of child fatalities: %d%n", numChildPassengers);

Simple. 简单。

Use one set of printf , and just set the variables you fill in there in the if/else branches. 使用printf ,只需在if / else分支中设置您要填充的变量即可。

In your code example, the ifs don't even do anything but change the first line that is printed. 在您的代码示例中,if甚至不执行任何操作,只是更改了要打印的第一行。

Only print out of the if/else , that will save you much. 仅打印出if/else ,这将为您节省很多。
You can also take the inner if/else out of the outer one. 您也可以将内部if/else移出外部。

if (Character.toUpperCase(choosePassOverPed) == 'Y') {
    passengerPoints = passengerPoints + toughCallThresholdPoints;
else {
    pedestrianPoints = pedestrianPoints + toughCallThresholdPoints;    
}
if (passengerPoints > pedestrianPoints){
    System.out.println("Fatality Group: PEDESTRIANS");
}
else {
    System.out.println("Fatality Group: OCCUPANTS");
}
System.out.printf("Number of adult fatalities: %d%n", numAdultPassengers);
System.out.printf("Number of child fatalities: %d%n", numChildPassengers);

I'm wouldn't actually write it like this but this gets rid of the duplicate code: 我实际上不会这样写,但这摆脱了重复的代码:

public void method() {
    // declare variables...

    if (Character.toUpperCase(choosePassOverPed) == 'Y') {
        passengerPoints = passengerPoints + toughCallThresholdPoints;
        checkPoints(passengerPoints, pedestrianPoints, numAdultPedestrians, numChildPedestrians, numAdultPassengers, numChildPassengers);
    } else {
        pedestrianPoints = pedestrianPoints + toughCallThresholdPoints;
        checkPoints(passengerPoints, pedestrianPoints, numAdultPedestrians, numChildPedestrians, numAdultPassengers, numChildPassengers);
    }
}

private static void checkPoints(int passengerPoints, int pedestrianPoints, int numAdultPedestrians, int numChildPedestrians, int numAdultPassengers, int numChildPassengers) {
    if (passengerPoints > pedestrianPoints) {
        System.out.println("Fatality Group: PEDESTRIANS");
        log(numAdultPedestrians, numChildPedestrians);
    } else {
        System.out.println("Fatality Group: OCCUPANTS");
        log(numAdultPassengers, numChildPassengers);
    }
}

private static void log(int numAdultPedestrians, int numChildPedestrians) {
    System.out.printf("Number of adult fatalities: %d%n", numAdultPedestrians);
    System.out.printf("Number of child fatalities: %d%n", numChildPedestrians);
}

I think there are some concepts in there that could further be abstracted, for example passengerPoints and pedestrians points both behave the same way so may be there is some sort of PersonPoints class that you need. 我认为其中有些概念可以进一步抽象,例如,passportPoints和walkerpoints的行为方式相同,因此可能需要某种PersonPoints类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM