网站建设资讯

NEWS

网站建设资讯

java判断质数的代码,Java怎么判断质数

java统计类中物理总行数中注释行,非注释行,以及代码的行数

import java.io.*;public class Check {

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名申请、网页空间、营销软件、网站建设、鼓楼网站维护、网站推广。

public static void main (String[] args) throws IOException{

check("D:/MyEclipse/java/src/my/Check.java");//这里的字符串是你要统计的文件的路径,你自己填写

} public static void check (String s) throws IOException{

int all = 0, empty = 0, describe = -1, i = 0;

String str = null;

File f = new File(s);

BufferedReader br = new BufferedReader (new FileReader(f));

str = br.readLine();

while(str != null){

all++;

if(str.trim().equals("")) empty++;

if(str.contains("//")) describe++;

if(str.contains("/*")){

while(!str.contains("*/")){

i++;

all++;

describe++;

str = br.readLine();

}

}

str = br.readLine();

}

System.out.println("文件物理总行数为:" + all);//;;klj

System.out.println("文件中空行数为:" + empty);//hkk

System.out.println("文件注释行数为:" + describe);

System.out.println("文件非注释行数为:" + (all - i));

/*asdfdsff

* sdasadfsf//fg

* asdfsdf//dsfg

* asdf

*/

}

}以上是代码,我在我的机子上实现了,希望能帮到你!我也是JAVA菜鸟,希望有高手能更好地解答

Java 有什么好的代码行数,注释行数统计工具

package com.syl.demo.test;

import java.io.*;

/**

* java代码行数统计工具类

* Created by 孙义朗 on 2017/11/17 0017.

*/

public class CountCodeLineUtil {

private static int normalLines = 0; //有效程序行数

private static int whiteLines = 0; //空白行数

private static int commentLines = 0; //注释行数

public static void countCodeLine(File file) {

System.out.println("代码行数统计:" + file.getAbsolutePath());

if (file.exists()) {

try {

scanFile(file);

} catch (IOException e) {

e.printStackTrace();

}

} else {

System.out.println("文件不存在!");

System.exit(0);

}

System.out.println(file.getAbsolutePath() + " ,java文件统计:" +

"总有效代码行数: " + normalLines +

" ,总空白行数:" + whiteLines +

" ,总注释行数:" + commentLines +

" ,总行数:" + (normalLines + whiteLines + commentLines));

}

private static void scanFile(File file) throws IOException {

if (file.isDirectory()) {

File[] files = file.listFiles();

for (int i = 0; i files.length; i++) {

scanFile(files[i]);

}

}

if (file.isFile()) {

if (file.getName().endsWith(".java")) {

count(file);

}

}

}

private static void count(File file) {

BufferedReader br = null;

// 判断此行是否为注释行

boolean comment = false;

int temp_whiteLines = 0;

int temp_commentLines = 0;

int temp_normalLines = 0;

try {

br = new BufferedReader(new FileReader(file));

String line = "";

while ((line = br.readLine()) != null) {

line = line.trim();

if (line.matches("^[//s[^//n]]*$")) {

// 空行

whiteLines++;

temp_whiteLines++;

} else if (line.startsWith("/*") !line.endsWith("*/")) {

// 判断此行为"/*"开头的注释行

commentLines++;

comment = true;

} else if (comment == true !line.endsWith("*/")) {

// 为多行注释中的一行(不是开头和结尾)

commentLines++;

temp_commentLines++;

} else if (comment == true line.endsWith("*/")) {

// 为多行注释的结束行

commentLines++;

temp_commentLines++;

comment = false;

} else if (line.startsWith("//")) {

// 单行注释行

commentLines++;

temp_commentLines++;

} else {

// 正常代码行

normalLines++;

temp_normalLines++;

}

}

System.out.println(file.getName() +

" ,有效行数" + temp_normalLines +

" ,空白行数" + temp_whiteLines +

" ,注释行数" + temp_commentLines +

" ,总行数" + (temp_normalLines + temp_whiteLines + temp_commentLines));

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (br != null) {

try {

br.close();

br = null;

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

//测试

public static void main(String[] args) {

File file = new File("F:\\myweb");

countCodeLine(file);

}

}

对于java语言来说,大量注释会影响执行效率么

Java代码注释写的多,会影响到编译效率,但是不会影响到执行效率。

Java代码是先编译成字节码,然后被JVM解释执行的。

我做了个实验

TimeDemo 类

import java.util.ArrayList;

public class TimeDemo {

public static void main(String[] args) {

long start = System.currentTimeMillis();

ArrayListInteger list = new ArrayListInteger();

for (int i = 0; i 1000000; i++) {

list.add(i);

}

long end = System.currentTimeMillis();

System.out.println("本次执行耗费了"+(end-start)+"毫秒");

}

}

TimeDemo2

import java.util.ArrayList;

public class TimeDemo2 {

public static void main(String[] args) {

long start = System.currentTimeMillis();

ArrayListInteger list = new ArrayListInteger();

for (int i = 0; i 1000000; i++) {

list.add(i);

}

//用java.io生成了很多行的注释,

//注释

//注释

//注释

//注释

//注释

long end = System.currentTimeMillis();

System.out.println("本次执行耗费了"+(end-start)+"毫秒");

}

}

运行结果

当注释行数是1~1万行的时候. 能较快的编译

当注释行数达到1百万的时候,编译稍微慢一点

当注释行数达到1千万行的时候, CPU占用100%,卡了进1分钟也没有编译完成,只好强行关闭

结论:

简单明了的注释有助于程序猿对代码的读写

只有当注释行数极大的时候,才会严重的影响编译速度。 但不会影响执行速度


网页标题:java判断质数的代码,Java怎么判断质数
文章来源:http://cdweb.net/article/hsijjj.html