复制循环,但结果不同。如何删除重复的代码

时间:2019-08-12 17:11:14

标签: java

我有重复的for循环。一种功能可以打印以屏蔽屏幕,另一种功能可以写入文件。由于for循环相同,但是结果不同,因此我很难找到避免重复的方法。

看来这个问题快要回答我自己的问题了;但是,据说在打印到屏幕上时可能会有些重复,我不确定是否是这种情况。

How to remove duplication from my code

private static void printToFile(char space, char hash, int spaceCount, int hashCount){
    try (PrintWriter outFile = new PrintWriter(new FileWriter("output.txt"))) {
        for (int i = 0; i < 8; i++) {
            outFile.write(String.valueOf(marioStepCreator(space, spaceCount)));
            outFile.flush();
            outFile.write(String.valueOf(marioStepCreator(hash, hashCount)));
            outFile.flush();
            outFile.write("\n");
            hashCount++;
            spaceCount--;
        }
    } catch(FileNotFoundException e) {
        e.getCause();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private static void printToScreen(char space, char hash, int spaceCount, int hashCount){
    for (int i = 0; i < 8; i++) {
        System.out.print(marioStepCreator(space, spaceCount));
        System.out.print(marioStepCreator(hash, hashCount));
        System.out.println();
        hashCount++;
        spaceCount--;
    }
}

5 个答案:

答案 0 :(得分:1)

如何?

private static void printToFileAndScreen(char space, char hash, int spaceCount, int hashCount){
    try (PrintWriter outFile = new PrintWriter(new FileWriter("output.txt"))) {
        for (int i = 0; i < 8; i++) {
            String spaceString = String.valueOf(marioStepCreator(space, spaceCount));
            String hashString = String.valueOf(marioStepCreator(hash, hashCount));
            outFile.write(spaceString);
            outFile.write(hashString);
            outFile.write("\n");
            outFile.flush();
            System.out.print(spaceString);
            System.out.print(hashString);
            System.out.println();
            hashCount++;
            spaceCount--;
        }
    } catch(FileNotFoundException e) {
        e.getCause();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

答案 1 :(得分:1)

您可以隔离for循环以生成String消息,该消息可以与writer或System.out.println一起打印在一行中。

private static void printToFile(char space, char hash, int spaceCount, int hashCount){
    try (PrintWriter outFile = new PrintWriter(new FileWriter("output.txt"))) {
        outFile.write(printMessage(space, hash, spaceCount, hashCount));
        outFile.flush();        
    } catch(FileNotFoundException e) {
        e.getCause();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private static void printToScreen(char space, char hash, int spaceCount, int hashCount){
    System.out.print(printMessage(space, hash, spaceCount, hashCount));
}

private static String printMessage(char space, char hash, int spaceCount, int hashCount) {
    String message = "";

    for (int i = 0; i < 8; i++) {
        message += marioStepCreator(space, spaceCount) + marioStepCreator(hash, hashCount) + "\n";
        hashCount++;
        spaceCount--;
    }

    return message;
}

答案 2 :(得分:1)

由于您已经在使用PrintWriter来写入文件,因此将通用代码移至辅助方法:

private static void printToFile(char space, char hash, int spaceCount, int hashCount){
    try (PrintWriter outFile = new PrintWriter(new FileWriter("output.txt"))) {
        printToWriter(outFile, space, hash, spaceCount, hashCount);
    } catch(FileNotFoundException e) {
        e.getCause();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private static void printToScreen(char space, char hash, int spaceCount, int hashCount){
    PrintWriter out = new PrintWriter(System.out);
    printToWriter(out, space, hash, spaceCount, hashCount);
    out.flush(); // Do not close
}

private static void printToWriter(PrintWriter out, char space, char hash, int spaceCount, int hashCount){
    for (int i = 0; i < 8; i++) {
        out.print(marioStepCreator(space, spaceCount));
        out.println(marioStepCreator(hash, hashCount));
        hashCount++;
        spaceCount--;
    }
}

当然,您非常需要在printToFile方法中修复异常处理。

由于使用try-with-resources立即关闭文件编写器,因此无需刷新输出。然而,PrintWriter包装的System.out必须被刷新,以防它正在缓冲,但不应关闭,因为我们不想关闭System.out

答案 3 :(得分:1)

您也可以尝试执行以下操作-

private static List<String> getOutput(char space, char hash, int spaceCount, int hashCount) {
    List<String> output = new ArrayList<>();
    for (int i = 0; i < 8; i++) {
        output.add(String.valueOf(marioStepCreator(space, spaceCount)));
        output.add(String.valueOf(marioStepCreator(hash, hashCount)));
        hashCount++;
        spaceCount--;
    }
    return output;
}

private static void printToFileAndScreen(char space, char hash, int spaceCount, int hashCount) {
    try (PrintWriter outFile = new PrintWriter(new FileWriter("output.txt"))) {
        getOutput(space, hash, spaceCount, hashCount).stream().forEach(s ->
        {
            outFile.write(s);
            outFile.flush();
        });
    } catch (FileNotFoundException e) {
        e.getCause();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private static void printToScreen(char space, char hash, int spaceCount, int hashCount) {
    getOutput(space, hash, spaceCount, hashCount).stream().forEach(s -> System.out.println(s));
}

这样,您可以从输出使用者中分离出主要业务逻辑。此外,您可以实现和接口,该接口具有针对不同编写者的“ write()”方法。这种方式很容易编写测试用例。

答案 4 :(得分:0)

如何?

private static void printToFile(char space, char hash, int spaceCount, int hashCount) {

    try (PrintWriter outFile = new PrintWriter(new FileWriter("output.txt"))) {
        loop(spaceCount, hashCount, (sCount, hCount) -> {
        outFile.write(String.valueOf(marioStepCreator(space, sCount)));
        outFile.flush();
        outFile.write(String.valueOf(marioStepCreator(hash, hCount)));
        outFile.flush();
        outFile.write("\n");
        });
    } catch (FileNotFoundException e) {
        e.getCause();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private static void printToScreen(char space, char hash, int spaceCount, int hashCount) {

    loop(spaceCount, hashCount, (sCount, hCount) -> {
        System.out.print(marioStepCreator(space, sCount));
        System.out.print(marioStepCreator(hash, hashCount));
        System.out.println();
    });
}

private static void loop(int spaceCount, int hashCount, final BiConsumer<Integer, Integer> consumer) {

    for (int i = 0; i < 8; i++) {
        consumer.accept(spaceCount, hashCount);
        hashCount++;
        spaceCount--;
    }
}