【问题标题】:having an if statement inside a loop without calling it in each iteration在循环内有一个 if 语句而不在每次迭代中调用它
【发布时间】:2016-09-09 17:01:02
【问题描述】:

我有以下代码:

    public static void main(final String[] args) {
    if (args.length == 0) {
        System.out.println("Please provide a correct directory path as an argument!");

    } else {

        System.out.println("Thanks for using our CodeMetrics\n"
                + "The process Might take a long time, please wait!\n"
                + "Please check the CSV file for the final results!");
        File ad = new File(args[0]);
        File[] list = ad.listFiles();

        for (File f : list) {

            CodeMetrics codeMetrics = new CodeMetrics();

            codeMetrics.parseCommandLine(f.toString());
            codeMetrics.countComplexity(codeMetrics.sourceCodeFile);

            // Count LOC (Lines of Code)
            codeMetrics.countLines(codeMetrics.sourceCodeFile);
            codeMetrics.countTestLines(codeMetrics.testFiles);

            codeMetrics.printReport();
            codeMetrics.writeReport();

        }

    }

}

现在我想让用户有机会选择调用printReport()方法或同时调用printReport()writeReport()。问题是如果我放置一个 if 语句,它将在 for each 循环中,并且用户必须为循环中的每个迭代进行选择。 我能看到的唯一想法是实现不同的方法,如下所示:

    public static void onlyPrint(final String args){

}

public static void printAndWrit (final String args){

}

这两种方法的代码相同,只是其中一种具有 printReport() 而另一种具有两种方法。但我对这个解决方案并不满意,因为我相信它会有很多代码冗余!有更好的解决方案吗?

谢谢

【问题讨论】:

  • 另一种说法是给用户写报告或不写报告的机会。在这两种情况下都会调用 print。
  • 在循环之前询问用户,并使用布尔值来判断他们是否需要一种方法或两种方法。然后在循环中的 if 语句中使用该值。

标签: java loops if-statement foreach


【解决方案1】:

Drew Kennedy 所说的很可能是最容易实现的答案。

public static void main(final String[] args) {
if (args.length == 0) {
    System.out.println("Please provide a correct directory path as an argument!");

} else {

    System.out.println("Thanks for using our CodeMetrics\n"
            + "The process Might take a long time, please wait!\n"
            + "Please check the CSV file for the final results!");
    File ad = new File(args[0]);
    File[] list = ad.listFiles();

   //ask the user here
   Scanner sc = new Scanner(System.in);

   System.out.println("Please enter a number : Would you like to (1) print, or (2) print & write?");
   int answer = scan.nextInt();
   boolean write = false;
   if (answer == 2) {
      write = true;
   }

    for (File f : list) {

        CodeMetrics codeMetrics = new CodeMetrics();

        codeMetrics.parseCommandLine(f.toString());
        codeMetrics.countComplexity(codeMetrics.sourceCodeFile);

        // Count LOC (Lines of Code)
        codeMetrics.countLines(codeMetrics.sourceCodeFile);
        codeMetrics.countTestLines(codeMetrics.testFiles);

        //check whether the user wants to write or not
        if (write == false) {
            codeMetrics.printReport();
        } else {
            codeMetrics.printReport();
            codeMetrics.writeReport();
        }
    }

}

}

这应该只询问用户一次,然后执行您需要完成的操作。

【讨论】:

    【解决方案2】:

    我同意 Childishforlife 的观点,即使用布尔值的解决方案是最简单的方法。我只想提一点优化。

    你可以转

     //check whether the user wants to write or not
        if (write == false) {
            codeMetrics.printReport();
        } else {
            codeMetrics.printReport();
            codeMetrics.writeReport();
        }
    

    进入:

        codeMetrics.printReport();
        if (write) {
            codeMetrics.writeReport();
        } 
    

    如果我理解一切正确,报告可以在两种情况下打印。只有写作取决于用户的选择。

    【讨论】:

    • 非常真实!这是一种比我更好的编码风格,我的有点多余。
    猜你喜欢
    • 2022-06-18
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 2014-12-08
    • 2017-07-20
    相关资源
    最近更新 更多