有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何计算JAVA中每个对象或函数的总LOC?

我需要计算对象或函数的位置。也就是说,对于java,每个对象中的方法数

其他要求是计算java代码的物理LOC和逻辑LOC,我已经做到了

我的代码正在读取一个。txt文件与bufferreader和计数的代码

我数了数物理、逻辑、空白和注释行。现在我需要计算方法和对象的位置

这样的预期产出

https://s8.postimg.org/cidz14y1h/Capture.png

对象名称#方法对象位置 XXX 555 YYY 19 ZZZ 3 14

你能帮我满足这个要求吗

public class HW2 {
public static void main(String[] args) {
    String lineToRead = "";                                                 //will be used to read the lines from .txt file
    int totalNumberofCommentLines = 0;                                      //will be used to count comment lines
    int totalNumberofBlankLines = 0;                                        //will be used to count blank lines
    int totalNumberofLines = 0;                                             //will be used to count all lines
    try {
        BufferedReader br = new BufferedReader(new FileReader("F:\\HW-1\\4.txt"));
        try {
            while ((lineToRead = br.readLine()) != null) {
                totalNumberofLines++;
                if (lineToRead.trim().isEmpty()) {
                    totalNumberofBlankLines++;
                }
                if (lineToRead.startsWith("//")) {                          //if the line starts with single line comment 
                    totalNumberofCommentLines++;                            //if yes, count it
                } else if (lineToRead.startsWith("/*")) {                   //if the line starts with multiple line comment
                    totalNumberofCommentLines++;                            //if yes, look at the content of it
                    do {
                        if (lineToRead.trim().isEmpty()) {                  //if there is a blank line, count it as blank
                            totalNumberofBlankLines++;
                        }
                        else
                        {    totalNumberofCommentLines++;                    //else, count it as comment line
                            totalNumberofLines++;
                        }
                    } while (!(lineToRead = br.readLine()).endsWith("*/")); //count until see multiple comment line closure
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(HW2.class.getName()).log(Level.SEVERE, null, ex);
        }
        br.close();                                                         //close the buffer reader to empty ram area
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println("Total Number of Physical Lines " + totalNumberofLines);
    System.out.println("Total Number of Comment Lines " + totalNumberofCommentLines);
    System.out.println("Total Number of Blank Lines " + (totalNumberofBlankLines));
    System.out.println("Total Number of Logical Lines " + (totalNumberofLines - ((totalNumberofBlankLines) + totalNumberofCommentLines)));
}

}


共 (0) 个答案