微軟實習面試-刪除一個JAVA檔案的全部注釋

來源:互聯網
上載者:User

標籤:

描述:刪除一個合法的JAVA檔案的全部注釋,注釋以"//"或者"/* */"表示。

注意: 單引號或者雙引號內的文字可能包括"//"或者"/* */",但不是注釋的部分。

解法:讀入檔案後,一個字元一個字元的處理。

 1 public void deleteAllComment(InputStream is) throws IOException { 2         int i = is.read(); 3         while(i != -1) { 4             char c = (char)i; 5             if(c == ‘/‘) { 6                 char j = (char)is.read(); 7                 if(j == ‘/‘) { 8                     deleteThisLine(is); 9                 } else if(j == ‘*‘) {10                     deleteUntilEnd(is);11                 }12             } else if(c == ‘\‘‘ || c == ‘"‘) {13                 echoString(is, c);14             } else {15                 System.out.print(c);16             }17             i = is.read();18         }19     }20     21     //刪除以"//"開頭的注釋22     private void deleteThisLine(InputStream is) throws IOException {23         char i = (char)is.read();24         char j = (char)is.read();25         while(true) {26             if(i == ‘\r‘ && j==‘\n‘) {27                 break;28             } else {29                 i = j;30                 j = (char)is.read();31             }32         }33     }34     35     //刪除以"/* */"表示的注釋36     private void deleteUntilEnd(InputStream is) throws IOException {37         char i = (char)is.read();38         char j = (char)is.read();39         while(true) {40             if(i == ‘*‘ && j==‘/‘) {41                 break;42             } else {43                 i = j;44                 j = (char)is.read();45             }46         }47     }48     49     //原樣輸出單引號或者雙引號中的內容50     private void echoString(InputStream is, int start) throws IOException {51         char end = (char)is.read();52         while(end != start) {53             System.out.print(end);54             end = (char)is.read();55         }56     }

 

微軟實習面試-刪除一個JAVA檔案的全部注釋

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.