为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

蝴蝶刀刀 2023-06-04 14:52:58
我有重复的 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--;    }}
查看完整描述

5 回答

?
临摹微笑

TA贡献1982条经验 获得超2个赞

由于您已经在使用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。


查看完整回答
反对 回复 2023-06-04
?
料青山看我应如是

TA贡献1772条经验 获得超7个赞

您可以隔离 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;

}


查看完整回答
反对 回复 2023-06-04
?
一只名叫tom的猫

TA贡献1906条经验 获得超2个赞

你也可以尝试做这样的事情 -


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()”方法的接口。这种方式很容易编写测试用例。


查看完整回答
反对 回复 2023-06-04
?
萧十郎

TA贡献1815条经验 获得超12个赞

这个怎么样:


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

    }

}


查看完整回答
反对 回复 2023-06-04
?
富国沪深

TA贡献1790条经验 获得超9个赞

这个怎么样:


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

    }

}


查看完整回答
反对 回复 2023-06-04
  • 5 回答
  • 0 关注
  • 161 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信