2014-11-21 85 views
-3

只是想知道如果我使用嵌套的if語句太多。我一直在環顧四周,似乎人們試圖不使用它們。代碼也看起來雜亂無章?無論如何它是這樣的:我的代碼是草率/糟糕嗎?

import java.util.Arrays; 

public class Main { 

private String user_input = ""; 
private int max_score = 6; 
private int sum; 

private void check_scores(String scores){ 
    user_input = scores; 
    String[] temp; 

    // Check if user_input is valid 
    //^Match with beginning of line | [0-9] Allow 0-9 | , Allow comma | + Match one or more | $ Match End of line 
    if (user_input.matches("^[0-9,]+$")) { 

     // Check if string starts with an , 
     if(user_input.charAt(0) == ',') { 
      // If it does parse and substring to remove them 
      // otherwise the following regex leaves one behind 
      int i = 0; 
      while (!Character.isDigit(user_input.charAt(i))) i++; 
      int j = user_input.length(); 
      user_input = user_input.substring(i,j); 
     } 

     // (.) Match any character) | \1 If it is followed by itself | + Match one or more | $1 replace by the first captured char. 
     user_input = user_input.replaceAll("(.)\\1+", "$1"); 

     System.out.println(user_input); 

     // Split at the ',' and put each number in it's own cell in the array 
     temp = user_input.split(","); 
     System.out.println(Arrays.toString(temp)); 

     // Check if temp is equal to max_scores 
     if (temp.length == max_score){ 
      int[] ui_array = new int[temp.length]; 

      // Parse String[] into int[] 
      for (int i = 0; i < temp.length; i++){ 
       try { 
        ui_array[i] = Integer.parseInt(temp[i]); 
       } catch (NumberFormatException nfe) {}; // If triple checking isn't enough... 
      } 
      System.out.println("temp array(String): " + Arrays.toString(temp)); 
      System.out.println("ui_array(int): " + Arrays.toString(ui_array)); 

      // Add up all elements in ui_array 
      for (int j = 0 ; j < ui_array.length; j++) { 
       sum += ui_array[j]; 
      } 
      System.out.println("Scores sum:" + sum + " Number of scores:" + ui_array.length + " Number of ends:" + ui_array.length/6); 
     } 
     else { 
      System.out.println("You have " + temp.length + " scores. Acceptable amount is " + max_score); 
     } 
    } 
    else { 
     System.out.println("Invalid Input. (Only #'s and ,'s allowed)"); 
    } 
} 

public static void main(String[] args) { 
    Main main = new Main(); 
    main.check_scores("1,M,7,10,,4,8,"); 
    main.check_scores("1,6,7,10,,4,8,,,,,,,1,2,6,10,2,10"); 
    main.check_scores(",,,,,,,1,2,6,10,2,10"); 
    main.check_scores("10,2,1,5,7,1"); 
    main.check_scores("6,2, ,,5,6,1"); 
    } 
} 

我剛纔一直想知道一段時間人們怎麼想我如何去做事情。

+8

這種問題更適合[CodeReview.SE](http://codereview.stackexchange.com/)。 – irrelephant 2014-11-21 05:25:09

+0

考慮重構該方法。 – 2014-11-21 05:26:44

+0

爲什麼重命名'scores'?如果你想叫它'user_input',只需重命名參數即可。此外,你應該在你的條件之後做一個新的線。您還應該將temp重命名爲有意義的內容;我使用temp的唯一時間就是它僅僅用於幾行中的小事,但你使用它很多,所以它應該有一個描述性的名字。我低估了這個問題,因爲就像不像大象說的那樣,這是CodeReview Stack Exchange的用處。 – 2014-11-21 05:43:37

回答

2

有幾件事情我想指出:

  1. 就個人而言,我認爲像

    的public void方法()
    {

    }

是多少比

更具可讀性
public void method() { 

} 

特別是當你有包含其他結構的方法時。我敢肯定,有些人可能不介意,但我從來沒有聽說有人說第一張不可讀,而很多人抱怨第二張。對不起,第一個沒有正確格式,但SO不會允許它..看起來網站管理員不同意我這一個。

  1. 這是標準變量,如someVariableName而不是some_variable_name。第一個詞應該是小寫字母,其他所有字母都應該是連續的。方法也是如此。

  2. 在你check_scores(String)方法你有user_input = scores;,但您的實現,沒有必要一個全局變量或傳遞變量指定的其他地方,所以這是浪費內存。實際上,因爲你的類變量沒有在這個方法的範圍之外使用,所以你應該在方法中聲明它們全部。我知道這是一個微不足道的例子,但爲了符合面向對象的編程思想,你的Main類可能應該放在一個單獨的文件中,並且可以通過在驅動程序類的main方法中創建一個對象來運行代替。

而且,正如你提到的,嵌套幾個語句,如果沒有必要,可以馬虎,因爲它會很快變得難以閱讀和遵守。

+1

不同意第一點 – tomasb 2014-11-21 05:44:55