簡體   English   中英

我怎么用不同的方法寫這個?

[英]How would I write this in separate methods?

我有一個java項目,我被告知寫一個程序,要求用戶輸入一些天和溫度。 從那里我必須找到平均溫度,高於平均溫度的天數,以及從最高到最低的溫度分類。 * 我已成功完成所有這些事情,但是,我的教授要求我通過創建單獨的方法來簡化我的代碼。 例如,有一個超過平均值的方法,等等。 雖然我理解這個概念,但我不確定如何接近並采用這種方式。 *請幫忙?

這是我到目前為止的代碼:

import java.util.Scanner;

public class NumberAboveAverage {

    /**
     * @param args
     */
    public static void main(String[] args) {

        System.out.println("Enter number of days of temperature to calculate:");
        Scanner keyboard = new Scanner(System. in);
        int day = keyboard.nextInt();
        int[] temperature = new int[day];

        System.out.println("Enter " + day + " temperatures to calculate.");

        for(int i=0; i<day; i++){
            temperature[i] = keyboard.nextInt();
        }

        int sum = 0;
        for(int i=0; i<day; i++){
            sum = sum + temperature[i];

        }

        int average = sum/day;
        System.out.println("The average temperature is: " + average); 

        int daysOver=0;
        for(int i=0; i<day; i++){
            if (temperature[i] > average){
                daysOver++;
            }   
        }
        System.out.println("The temperature was above average for " + daysOver + " day(s).");


            for (int i = 0; i < temperature.length; i++) {
                int min = i;
                for (int j = i; j < temperature.length; j++) {
                    if (temperature[j] < temperature[min])
                        min = j;
                }
                int temp;
                temp = temperature[i];
                temperature[i] = temperature[min];
                temperature[min] = temp;
            }

    System.out.print("The temperatures in increasing order are: ");
    for(int i=0; i<day; i++){
        System.out.print(temperature[i]+" ");
    }

}   
}

分解代碼的好方法是用英語進行相同的分解! 你自己說的:

編寫一個程序,要求用戶輸入若干天和溫度。 從那里我必須找到平均溫度,高於平均溫度的天數,以及從最高到最低的溫度分類。

  • 要求用戶輸入若干天和溫度
  • 找到平均溫度
  • 對tempature進行排序

然后我們可以把它變成代碼!

public static void main(String []args) {
  int[] temperature = getUserInput();
  int average = getAverage(temperature);
  int daysOver = getDaysOver(temperature, threshold);

  System.out.println("Average of : " + average + " with " + daysOver + " really hot days");
}

現在您可以將代碼放在這些函數中,並打印出您喜歡的任何內容:)

將代碼分解為每個解決難題的步驟。 例如,有一種計算平均溫度的方法,另一種計算高於平均溫度的天數的方法,以及另一種按升序排序溫度的方法。

而不是在代碼中回答你的問題,我會給你一些指示。

您可以抽象出一些功能。

  • 計算平均值的函數。
  • 計算平均天數的函數。
  • 一種對溫度進行分類的功能。
  • 按順序打印溫度的功能。

您可能希望在單獨的方法中執行以下任務:

 public static void main(String[] args) {
    int[] temperature = new int[1];
    Scanner keyboard = new Scanner(System. in);
    readTemperatures(keyboard, temperature);
    int sum = getSum(temperature);
    int average = sum/temperature.length;
    System.out.println("The average temperature is: " + average); 
    int daysOver=getDaysOver(temperature, average);
    System.out.println("The temperature was above average for " 
                                                         + daysOver + " day(s).");
    sortTemperature(temperature);
    printTemperature(temperature);
 }

並創建如下方法:

private static void printTemperature(int[] temperature) {
    System.out.print("The temperatures in increasing order are: ");
     for(int i=0; i<temperature.length; i++){
        System.out.print(temperature[i]+" ");
     }
}

private static void readTemperatures(Scanner keyboard, int[] temperature){
       System.out.println("Enter number of days of temperature to calculate:");
       int day = keyboard.nextInt();
       temperature = new int[day];
       System.out.println("Enter " + day + " temperatures to calculate.");
       for(int i=0; i<day; i++){
        temperature[i] = keyboard.nextInt();
       }
}

完成其余的方法。

好吧,對於初學者來說,選擇一些相當完整的陳述序列,例如:

int daysOver=0;
for(int i=0; i<day; i++){
    if (temperature[i] > average){
        daysOver++;
    }   
}

找出您需要輸入的參數以及您需要的結果:

  • 一天(也許應該叫“numDays”,但這是另一個討論)
  • 溫度[]
  • 閾值(平均值)
  • 返回daysOver

(對於nonce,我忽略了復雜性/“優化”,你可以從數組的大小得到day 。)

所以,方法可能是:

computeDaysOver(int[] temperature, int average, int day) {
    int daysOver=0;
    for(int i=0; i<day; i++){
        if (temperature[i] > average){
            daysOver++;
        }   
    }
 }

但是你需要指定一個返回類型,並實際返回它:

int computeDaysOver(int[] temperature, int average, int day) {
    int daysOver=0;
    for(int i=0; i<day; i++){
        if (temperature[i] > average){
            daysOver++;
        }   
    }
    return daysOver;
 }

當然,您需要將新方法替換為原始代碼的位置:

int daysOver = computeDaysOver(temperature, average, day);

請注意,在選擇要包含在函數中的操作時,使用有限數量的輸入和輸出以及明確定義的目的“繪制圓周”操作非常重要。 嘗試包含一整套連接操作,而不僅僅是一組任意設置。

例如,您可以編寫一個包含求和循環和除法的computeAverage函數。 除非有一些單獨的需求來計算求和,否則通常不會在函數和除外(或在單獨的函數中)中的求和循環。 並且,如果由於某種原因需要將求和作為單獨的函數,則需要使您的computeAverage函數調用求和函數然后進行除法,而不是將求和作為參數傳遞。

(在這種情況下,這些示例相當簡單,但原則會延續到更大,更復雜的代碼塊。)

那么為什么人們會將他的代碼分解成不同的方法呢? 這是因為為了分離不同的不同邏輯/要求,使code becomes readable and bug hunting becomes easy

在你的情況下,你有這些事情要做:

calculating the average will go into separate method.
sorting the temperatures-will go into separate method..
A method that prints temperatures in order.

因此,方法具有返回類型。 這意味着經過計算並經過邏輯后,它會帶來一些輸出,所以我們可以使用返回類型的方法來獲取這個值。

在此之后使用可以使你的類的對象像這樣

MyClass myClassObj = new MyClass();

現在使用像這樣的引用變量myClassObj來調用你的方法

myClassObj.myMethod();

如果返回某些內容,則將其保存到變量中。

因此,如果像myMethod()返回一個String值,那么我們可以這樣做,以便在調用方法中獲得myMethod()的結果:

String myStrVar = myClassObj.myMethod();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM