import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class test { static int normaline = 0;
static int commentline = 0;
static int whiteline = 0;

public static void main(String arg[]){
File f = new File("E:\\javaworkspace\\noname\\src\\sdf");
File[] list = f.listFiles(); for(File shit : list)
{

           if(shit.getName().matches(".*\\.java$"));
           {
                   tongji(shit);
               }
}
    System.out.println("正常的代码行数"+normaline);
     System.out.println("注释的行数"+commentline);
     System.out.println("空白的行数"+whiteline);
} private static void tongji(File f) {
BufferedReader bf = null;
boolean comment= false;
try {
bf = new BufferedReader(new FileReader(f));
String line = "";
while((line=bf.readLine())!= null)
{
line = line.trim();
if(line.matches("^[\\s&&[^\\n]]*$")){
whiteline++;
}else if(line.startsWith("/*")&&!line.endsWith("*/")){
commentline++;
comment = true;
}else if(line.startsWith("//"))
{
commentline++;

}else if (comment == true){
commentline++; 
if(line.endsWith("*/")){
comment = false;
}
}else {
normaline++;
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bf!=null){
try {
bf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

}
} if(shit.getName().matches(".*\\.java$"));
           {
                   tongji(shit);
               }
这里如果换成了while就不能显示了???为什么讷讷?????求大神解答

解决方案 »

  1.   

    if(shit.getName().matches(".*\\.java$"));为什么后面会有个分号,因为这个分号,这个if语句就是费的,改while就是死循环。
      

  2.   

    while是循环,if只是判断,
    比如你的第一个shit文件名就是以.java结尾的。
    也就是shit.getName().matches(".*\\.java$") == true
    而用while会死循环吧
      

  3.   

    我很好奇,你代码中if 判断后有分号,能跑起来吗?
    如果想用while 把 for 替换掉不就行了