Reference: https://segmentfault.com/q/1010000012636380/a-1020000012640905
Title: In the Statistics file//And/* * * * Number of comments, double quotes do not count
Importjava.util.ArrayList;ImportJava.util.Scanner; Public classNotecounter { Public Static voidMain (string[] args)throwsException {//TODO Auto-generated method stubsScanner in=NewScanner (system.in); ArrayList<String> ve=NewArraylist<string>(); while(In.hasnext ()) {String temp=In.nextline (); Ve.add (temp); if(Temp.equals ("}")) Break; } System.out.println (VE); System.out.println (Operatenote (VE) [0]+ "" +operatenote (ve) [1]); } Public Static int[] operatenote (arraylist<string> list)throwsexception{String S=NULL; intCountnote=0; intCharinnote=0; for(intJ=0;j<list.size (); j + +) {s=List.get (j); intNote1=s.indexof ("/*"); intNote2=s.indexof ("//"); intNote3=s.indexof ("* *"); //int Note4=s.indexof ("\" ");String DM="\"(.*)\"";//Double quotesString sm= "\ ' (. *) \ '";//single quotation marks if(Note1!=-1&¬e3==-1) {//Multi-line commentscountnote++; String TTT=List.get (j); List.set (J, Ttt.substring (0, note1)); Charinnote+=s.substring (note1). Length () +1;//+1 is include line breaks=list.get (+ +)j); while((Note3=s.indexof ("*/")) ==-1) { if((Note2=s.indexof ("//"))!=-1) {Countnote++; } list.remove (j); Charinnote+=s.length (+1); if(J<list.size ()-1) {s=list.get (+ +)j); }Else { Break; }} list.remove (j); Charinnote+=s.length (); }Else if(Note2!=-1) {// "//"single-line comment for classcountnote++; List.set (J, S.substring (0, Note2)); Charinnote+=s.substring (note2). Length () +1; }Else if(Note1!=-1&¬e3!=-1) {//single-line commentcountnote++; String M1=s.substring (0, note1); String m2=s.substring (note3+2); String M3=m1+m2; Charinnote+=s.substring (Note1, note3+2). Length (); List.set (J, M3); }Else{//Delete Output StatementsString rp=List.get (j); RP=rp.replaceall (DM, ""); List.set (J, RP); } } return New int[]{countnote,charinnote}; }}
Test data:
///**/* */int main ( ) { Char[] s="/* String * *"; return 0 ;}
Output Result:
3 (number of Notes) 20 (number of characters in the note)
Package Toutiao;
Import Java.util.arraylist;import Java.util.Scanner;
public class Notecounter {
public static void Main (string[] args) throws Exception {//TODO auto-generated method stub Scanner in=new Scanner (system.in); arraylist<string> ve=new arraylist<string> (); while (In.hasnext ()) { String temp=in.nextline (); Ve.add (temp); &NBSP ; if (Temp.equals ("}")) break; } System.out.println (VE); SYSTEM.OUT.PRINTLN (Operatenote (VE) [0]+ " " +operatenote (ve) [1]);} public static int[] Operatenote (arraylist<string> list) throws exception{ & nbsp string s = null; int countnote=0; int charinnote=0; for (int j=0;j<list.size (); j + +) { s=list.get (j); &NB Sp int note1=s.indexof ("/*"); int note2=s.indexof ("//"); int Note3=s.indexof ( "*/"); //int note4=s.indexof ("\" ");
string dm= "\" (. *) \ "";//double quotes string sm= "\" ( . *) \ ' ";//single quote if (note1!=-1&¬e3==-1) {//multiline comments count note++; String Ttt=list.get (j); & nbsp List.set (J, ttt.substring (0, note1)); charinnote+=s.substring (note1). Length () +1;//+1 is include line break &NBSP;&NBS p; S=list.get (++j); while (Note3=s.indexof ("* *") ==-1) { if (note 2=s.indexof ("//"))!=-1) { countnote++; &NB Sp } List.remo ve (j); Charinnote+=s.length () +1; if (j<list . Size ()-1) { S=list.get (++j); &NB Sp }else { &NB Sp break; } }&N Bsp List.remove (j); Charinnote+=s.le Ngth ();Single-line note for }else if (note2!=-1) {//"//" class countnote++; List.set (J, S.substring (0,note2)); charinnote+=s.substring (note2). Length () +1; }else if (note1!=-1&¬e3!=-1) {//single-line comments countnote++;
String m1=s.substring (0, note1); String m2=s.substring (note3+2); String m3=m1+m2; Charinnote+=s.substring (Note1, note3+2). Length (); List.set (J, M3); }else {//delete output statement String rp=list.get (j); Rp=rp.replaceall (DM, ""); List.set (J, RP); }} return new Int[]{countnote,charinnote};
}
}
Java Statistics file Comment number