2012-12-04 22 views
2

儘管仍然試圖在Java中進行編程,但下面是我已經提交給大學的多種方法中最近一次賦值的代碼。簡化Java中多種方法的代碼?

我的查詢是,是否有可能簡化代碼任何使其更有效,而不是通過較長的路線走。

1:打印數組的最大值。
2:打印陣列的最低值。
3:打印數組的平均值。
4:打印字符串中特定單詞的出現次數。
5:打印字符串的平均字長。

public class MaxMinAverage { 
static int[] values = {1, 4, 3, 57, 7, 14, 7, 3, 10, 5, 4, 4, 10, 5, -88}; 
static String sentence = "the cat sat on the mat and the dog sat on the rug"; 
public static void main(String[] args) { 
    System.out.println("MaxMinAverage.java\n====================="); 
    System.out.println("Maximum value = "+getMaximum(values)); 
    System.out.println("Minimum value = "+getMinimum(values)); 
    System.out.println("Average Value =" +getAverage(values)); 
    System.out.println("Frequency of 'the' = "+getFrequency(sentence,"the")); 
    System.out.println("Average word length = "+getAverageWordLength(sentence)); 
    } 
    public static int getMaximum(int[]arr){ 
     int max = 0; 
     for(int i = 0; i < values.length; i++){ 
      if(values[i] > max){ 
        max = values[i]; 
        } 
      } 
     return max; 
    } 
    public static int getMinimum(int[] arr){ 
     int min = 0; 
     for(int i = 1; i < values.length; i++){ 
      if(values[i] < min){ 
        min = values[i]; 
        } 
      } 
     return min; 
    } 
    public static float getAverage(int[] arr){ 
     float result = 0; 
     for(float i = 0; i < values.length; i++){ 
      result = result + values[(int) i]; 
     } 
     return result/values.length; 
    } 
    public static int getFrequency(String sentance, String word){ 
     String keyword = "the"; 
     String[] temp; 
     String space = " "; 
     temp = sentence.split(space); 
     int counter = 0; 
     for(int i = 0; i < temp.length; i++){ 
      if(temp[i].equals(keyword)){ 
       counter++; 
      } 
     } 
     return counter; 
    } 
    public static float getAverageWordLength(String sentance){ 
     String characters = sentence.replaceAll("\\W",""); 
     float total = characters.length(); 
     float result = 0; 
     String[] temp; 
     String space = " "; 
     temp = sentence.split(space); 
     for(int i = 0; i < temp.length; i++){ 
      result++; 
     } 
     return total/result; 
    } 
} 
+0

當前的代碼有什麼問題?它看起來很好。 –

+0

您需要分別將'min'和'max'的初始值設置爲'Integer.MAX_VALUE'和'Integer.MIN_VALUE'。 –

+0

如果您正在尋找簡化步驟,請點擊此處http://codereview.stackexchange.com/?as=1 –

回答

0

使其更加有效:

你總是可以使用DRY(不要重複自己)這裏創建一個ArrayUtils類,並把所有這些方法存在和推廣,從而使您可以重用他們。

public static int getMinimum(int[] arr){ 
     int min = arr[0]; //change here 
     for(int i = 1; i < values.length; i++){ 
      if(values[i] < min){ 
        min = values[i]; 
        } 
      } 
     return min; 
    } 

max方法類似的變化

+0

謝謝,當然對我來說開始下一個工作。 –

0

getMaximum你可能想int max = Integer.MIN_VALUE;並在getMinimum你想int min = Integer.MAX_VALUE;。否則getMaximum將返回0,如果數組中的所有元素都小於零(因此您返回的值不在數組中),並且在getMinimum中,如果所有元素都大於零(這也是錯誤的),您將返回0。

此外,在getMinimum開始從指數1迭代意味着你錯過指數0

此外,您不使用這些方法的參數,可以直接使用values陣列。假設你調用getMinimum(someOtherArray),你仍然可以計算values。相反,你應該迭代給出的參數像這樣:

public static int getMinimum(int[] arr){ 
    int min = Integer.MAX_VALUE; 
    for(int i = 0; i < arr.length; i++){ 
     if(arr[i] < min){ 
       min = arr[i]; 
     } 
    } 
    return min; 
} 

這當然應該爲所有方法完成。

0

你可以放getMaximumgetMinimumgetAverage在同一個循環的邏輯部分(同在一個又一個getFrequencygetAverageWordLength)如下:

public static void getMinMaxAvg(int[] values) { 
    if (values == null || values.length == 0) { 
     throw new IllegalArgumentException(); 
    } 
    int min = values[0]; 
    int max = values[0]; 
    int i = 1; 
    int sum = 0; 
    for (i = 1; i < values.length; i++) { 
     if (values[i] < min) { 
      min = values[i]; 
     } else if (values[i] > max) { 
      max = values[i]; 
     } 
     sum += values[i]; 
    } 
    System.out.println("min = " + min); 
    System.out.println("max = " + max); 
    System.out.println("avg = " + ((float) sum/(i + 1))); 
} 

public static void getFreqAvg(String sentence, String word) { 
    if (sentence == null || sentence.isEmpty()) { 
     throw new IllegalArgumentException(); 
    } 
    if (word == null || word.isEmpty()) { 
     throw new IllegalArgumentException(); 
    } 
    String[] words = sentence.replaceAll("^ *(.*?) *$", "$1").split(" +"); 
    int freq = 0; 
    int sum = 0; 
    int i = 0; 
    for (i = 0; i < words.length; i++) { 
     if (words[i].equalsIgnoreCase(word)) { 
      freq++; 
     } 
     sum += words[i].length(); 
    } 
    System.out.println("freq of \"" + word + "\" = " + freq); 
    System.out.println("avg word length = " + ((float) sum/(i + 1))); 
} 

public static void main(String[] args) { 

    int[] values = { 1, 4, 3, 57, 7, 14, 7, 3, 10, 5, 4, 4, 10, 5, -88 }; 
    String sentence = " the cat sat on the mat and the dog sat on the rug "; 
    String word = "the"; 

    getMinMaxAvg(values); 
    getFreqAvg(sentence, word); 

} 

打印:

min = -88 
max = 57 
avg = 2.8125 
freq of "the" = 4 
avg word length = 2.642857