2013-08-01 40 views
-2

我想就某件事發表您的意見。我已經進行了大約一年的編程,並且我有一種感覺,也是描述性的,在我留在我的程序中的評論中。所以,我要給你一個小程序,我寫的,我想你給我一些反饋:)在評論中太過描述

import java.util.Scanner; 

public class ListWordsInString { 

public static void main(String[] args) { 

    Scanner stdin = new Scanner(System.in); 
    String str; 
    String word; 

    System.out.println("Enter a sentence: "); 
    str = stdin.nextLine(); 
    str = str.toLowerCase(); 

    // The following code goes through the string entered by the user (str) 
    // until it reaches a word. A word is considered any grouping of letters 
    // and apostrophes. For example: (that's, 'round, truck, etc.) 
    // Then it adds the current and the following characters of the string (str) 
    // in a newly created one called "word". It stops adding characters when 
    // it reaches an illegal one (that'd be one that is NOT a letter OR a apostrophe) 
    // or the end of the string. Then it prints the word and starts to search for another one. 
    for (int i = 0; i < str.length(); i++) { 
     word = ""; 
     while (str.charAt(i) >= 'a' && str.charAt(i) <= 'z' || str.charAt(i) == '\'') { 
       word += str.charAt(i); 
       i++; 
       if (i == str.length()) { 
        break; 
       } 
     } 
     if (!word.equals("")) { 
      System.out.println(word); 
     } 
    } // for() 
} // main() 

} 

PS:英語不是我的第一語言有關的任何拼寫或語法錯誤很抱歉。

+4

你的問題是在這裏的題目,可能會被關閉,去[代碼評論網站](http://codereview.stackexchange.com/)。你很可能會在那裏得到答案。 – morgano

+2

而不是方法內部的牆文本,儘量做到簡潔和精確,並使用[Javadoc](http://www.oracle.com/technetwork/java/javase/documentation/index-jsp-135444.html)。 – araknoid

+0

...是的,通過指定你的__for__循環和__main__函數結束的地方,你太冗長了 – morgano

回答

1

Javadoc是你的朋友,擁抱它。

+1

Javadoc +命名變量>>全部評論:) – NiziL