如何计算存储在文本文件中的程序的Java注释?
您可以使用Scanner类读取文件的内容,也可以使用contains()方法在特定行中找到注释。
示例
import java.io.*;
import java.util.Scanner;
public class FindingComments {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new File("HelloWorld"));
String input;
int single = 0;
int multiLine = 0;
while (sc.hasNextLine()) {
input = sc.nextLine();
if (input.contains("/*")) {
multiLine ++;
}
if(input.contains("//")) {
single ++;
}
}
System.out.println("no.of single line comments ::"+single);
System.out.println("no.of single line comments ::"+multiLine);
}
}文件HelloWorld的内容-
Public class SampleProgram{
/* This is my first java program.
* This will print ‘Hello World’ as the output
*/
Public static void main(String args[]){
//打印HelloWorld-
System.out.println("Hello World");
}
}输出结果
no.of single line comments ::1 no.of single line comments ::1
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短