gpt4 book ai didi

java - cloc,如何对结果求和?

转载 作者:行者123 更新时间:2023-11-30 07:34:26 24 4
gpt4 key购买 nike

我正在开发一个程序来分析程序的源代码。现在,我在计算结果时遇到了麻烦,这是我的源代码:

public void walk(String path) throws FileNotFoundException {

File root = new File(path);
File[] list = root.listFiles();
int countFiles = 0;

if (list == null) {
return;
}

for (File f : list) {
if (f.isDirectory()) {
walk(f.getAbsolutePath());
}

if (f.getName().endsWith(".java")) {
System.out.println("File:" + f.getName());
countFiles++;
Scanner sc = new Scanner(f);
int count = 0;
while (sc.hasNextLine()) {

count++;
sc.nextLine();

}
Scanner sc2 = new Scanner(f);
int lower = 0;
int upper = 0;
int digit = 0;
int whiteSpace = 0;
while (sc2.hasNextLine()) {
String str = sc2.nextLine();

for (int i = 0; i < str.length(); i++) {
if (Character.isLowerCase(str.charAt(i))) {
lower++;
} else if (Character.isUpperCase(str.charAt(i))) {
upper++;
} else if (Character.isDigit(str.charAt(i))) {
digit++;
} else if (Character.isWhitespace(str.charAt(i))) {
whiteSpace++;

}
}
}

System.out.println("Your code contains: " + count + " Lines!, Out of them:");
System.out.println("lower case: " + lower);
System.out.println("upper case: " + upper);
System.out.println("Digits: " + digit);
System.out.println("White Spaces: " + whiteSpace);

}
System.out.println("You have in total: " + countFiles);
}

}

第一个问题:当涉及到 countFiles(它应该告诉你的代码中有多少个 .java 文件或类)时,它的计数和打印结果如下:你总共有 = 1 个文件你总共有 = 2 个文件那么我怎样才能让它直接打印最终结果,在本例中是 2 ?

第二个问题:如何打印代码中所有行的总和,而不是单独显示每个类的行数?

谢谢

最佳答案

已通过在 for 循环外部添加 if 语句来解决此问题,如下所示:

     if(!(countFiles<=0) ){

System.out.println("You have in total: " + countFiles);


}

关于java - cloc,如何对结果求和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35602615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com