作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个程序来分析程序的源代码。现在,我在计算结果时遇到了麻烦,这是我的源代码:
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/
我是一名优秀的程序员,十分优秀!