重复 for 循环但结果不同。我如何删除重复代码

我有重复的 for 循环。一个函数打印到屏幕,另一个函数写入文件。由于 for 循环相同,但结果不同,我很难找到避免重复的方法。

看来这个问题接近于回答我自己的问题;然而,据说当打印到屏幕上时,它们可能会出现一些重复,我不确定这里是否是这种情况。

如何从我的代码中删除重复项

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

    }

}


蝴蝶刀刀
浏览 211回答 5
5回答

临摹微笑

由于您已经在使用PrintWriter用于写入文件,因此将通用代码移至辅助方法:private static void printToFile(char space, char hash, int spaceCount, int hashCount){&nbsp; &nbsp; try (PrintWriter outFile = new PrintWriter(new FileWriter("output.txt"))) {&nbsp; &nbsp; &nbsp; &nbsp; printToWriter(outFile, space, hash, spaceCount, hashCount);&nbsp; &nbsp; } catch(FileNotFoundException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.getCause();&nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}private static void printToScreen(char space, char hash, int spaceCount, int hashCount){&nbsp; &nbsp; PrintWriter out = new PrintWriter(System.out);&nbsp; &nbsp; printToWriter(out, space, hash, spaceCount, hashCount);&nbsp; &nbsp; out.flush(); // Do not close}private static void printToWriter(PrintWriter out, char space, char hash, int spaceCount, int hashCount){&nbsp; &nbsp; for (int i = 0; i < 8; i++) {&nbsp; &nbsp; &nbsp; &nbsp; out.print(marioStepCreator(space, spaceCount));&nbsp; &nbsp; &nbsp; &nbsp; out.println(marioStepCreator(hash, hashCount));&nbsp; &nbsp; &nbsp; &nbsp; hashCount++;&nbsp; &nbsp; &nbsp; &nbsp; spaceCount--;&nbsp; &nbsp; }}当然,您真的需要修复printToFile方法中的异常处理。由于您使用 try-with-resources 立即关闭文件编写器,因此无需刷新输出。然而,包装必须被刷新,以防它正在缓冲,但不应该被关闭,PrintWriter因为System.out我们不想关闭System.out。

料青山看我应如是

您可以隔离 for 循环以生成一个 String 消息,该消息可以使用 writer 或 System.out.println 在一行中打印;private static void printToFile(char space, char hash, int spaceCount, int hashCount){&nbsp; &nbsp; try (PrintWriter outFile = new PrintWriter(new FileWriter("output.txt"))) {&nbsp; &nbsp; &nbsp; &nbsp; outFile.write(printMessage(space, hash, spaceCount, hashCount));&nbsp; &nbsp; &nbsp; &nbsp; outFile.flush();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; } catch(FileNotFoundException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.getCause();&nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}private static void printToScreen(char space, char hash, int spaceCount, int hashCount){&nbsp; &nbsp; System.out.print(printMessage(space, hash, spaceCount, hashCount));}private static String printMessage(char space, char hash, int spaceCount, int hashCount) {&nbsp; &nbsp; String message = "";&nbsp; &nbsp; for (int i = 0; i < 8; i++) {&nbsp; &nbsp; &nbsp; &nbsp; message += marioStepCreator(space, spaceCount) + marioStepCreator(hash, hashCount) + "\n";&nbsp; &nbsp; &nbsp; &nbsp; hashCount++;&nbsp; &nbsp; &nbsp; &nbsp; spaceCount--;&nbsp; &nbsp; }&nbsp; &nbsp; return message;}

一只名叫tom的猫

你也可以尝试做这样的事情 -private static List<String> getOutput(char space, char hash, int spaceCount, int hashCount) {&nbsp; &nbsp; List<String> output = new ArrayList<>();&nbsp; &nbsp; for (int i = 0; i < 8; i++) {&nbsp; &nbsp; &nbsp; &nbsp; output.add(String.valueOf(marioStepCreator(space, spaceCount)));&nbsp; &nbsp; &nbsp; &nbsp; output.add(String.valueOf(marioStepCreator(hash, hashCount)));&nbsp; &nbsp; &nbsp; &nbsp; hashCount++;&nbsp; &nbsp; &nbsp; &nbsp; spaceCount--;&nbsp; &nbsp; }&nbsp; &nbsp; return output;}private static void printToFileAndScreen(char space, char hash, int spaceCount, int hashCount) {&nbsp; &nbsp; try (PrintWriter outFile = new PrintWriter(new FileWriter("output.txt"))) {&nbsp; &nbsp; &nbsp; &nbsp; getOutput(space, hash, spaceCount, hashCount).stream().forEach(s ->&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outFile.write(s);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outFile.flush();&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; } catch (FileNotFoundException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.getCause();&nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}private static void printToScreen(char space, char hash, int spaceCount, int hashCount) {&nbsp; &nbsp; getOutput(space, hash, spaceCount, hashCount).stream().forEach(s -> System.out.println(s));}通过这种方式,您可以将主要业务逻辑与输出消费者分开。此外,您可以为不同的编写器实现和连接具有“write()”方法的接口。这种方式很容易编写测试用例。

萧十郎

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

富国沪深

这个怎么样:private static void printToFile(char space, char hash, int spaceCount, int hashCount) {&nbsp; &nbsp; try (PrintWriter outFile = new PrintWriter(new FileWriter("output.txt"))) {&nbsp; &nbsp; &nbsp; &nbsp; loop(spaceCount, hashCount, (sCount, hCount) -> {&nbsp; &nbsp; &nbsp; &nbsp; outFile.write(String.valueOf(marioStepCreator(space, sCount)));&nbsp; &nbsp; &nbsp; &nbsp; outFile.flush();&nbsp; &nbsp; &nbsp; &nbsp; outFile.write(String.valueOf(marioStepCreator(hash, hCount)));&nbsp; &nbsp; &nbsp; &nbsp; outFile.flush();&nbsp; &nbsp; &nbsp; &nbsp; outFile.write("\n");&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; } catch (FileNotFoundException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.getCause();&nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}private static void printToScreen(char space, char hash, int spaceCount, int hashCount) {&nbsp; &nbsp; loop(spaceCount, hashCount, (sCount, hCount) -> {&nbsp; &nbsp; &nbsp; &nbsp; System.out.print(marioStepCreator(space, sCount));&nbsp; &nbsp; &nbsp; &nbsp; System.out.print(marioStepCreator(hash, hashCount));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; });}private static void loop(int spaceCount, int hashCount, final BiConsumer<Integer, Integer> consumer) {&nbsp; &nbsp; for (int i = 0; i < 8; i++) {&nbsp; &nbsp; &nbsp; &nbsp; consumer.accept(spaceCount, hashCount);&nbsp; &nbsp; &nbsp; &nbsp; hashCount++;&nbsp; &nbsp; &nbsp; &nbsp; spaceCount--;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java