Mam projekt java, w którym kazano mi napisać program, który prosi użytkownika o wprowadzenie liczby dni i temperatur. Stamtąd muszę znaleźć średnią temp, dni powyżej średniej temperatury, a także sortowanie temperatur od najwyższej do najniższej. * Z powodzeniem wykonałem wszystkie te czynności, jednak mój profesor prosi mnie, abym uprościł mój kod, tworząc oddzielne metody. Na przykład, mając metodę na dni powyżej średniej i tak dalej. Chociaż rozumiem koncepcję tego, nie jestem pewien, jak podejść i iść w ten sposób. * Proszę o pomoc?Jak to napisać w osobnych metodach?
Oto kod, który mam do tej pory:
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]+" ");
}
}
}
Będziesz także chciał przenieść (prawie) wszystko z 'main()' i utworzyć ważne zmienne pola instancji. – Dmitri