2017-02-24 11 views
2

Ich versuche ein Problem für diese Aufgabe zu lösen, die mir für Hausaufgaben gegeben wurde. Ich bin derzeit fest und würde jede Hilfe schätzen, die mich bei der Korrektur des Programms führen könnte.Inkompatible Typen: double [] [] kann nicht in double umgewandelt werden

Die ursprüngliche Zuordnung ist wie folgt:

ein Programm schreiben, die eine zweidimensionale Anordnung verwendet für jeden Monat des Jahres die höchsten und niedrigsten Temperaturen zu speichern. Schreiben Sie zwei Methoden: eine für die Berechnung und Rückgabe des Durchschnitts hoch und eine für die Berechnung und Rückgabe des durchschnittlichen Jahrestiefs. Ihr Programm sollte alle Werte im Array ausgeben und dann das durchschnittliche Hoch und das durchschnittliche Tief ausgeben.

Dies ist der Code, den ich bisher zusammengestellt habe und einen Fehler habe, den ich nicht lösen kann. Es ist "inkompatible Typen:. Umwandlung double [] [] kann nicht zu verdoppeln umgewandelt werden, um die Linien in Frage sind Linie 8 und Linie 110 (die letzte Rückkehr in das Programm)

import java.util.*; 

public class Weather 
{ 
public static void main(String[] args) 
    { 

    double[][] tempData = getData(); 
    printTempData(tempData); 
    double avgHigh = averageHigh(tempData); 
    double avgLow = averageLow(tempData); 
    int indexHigh = indexHighTemp(tempData); 
    int indexLow= indexLowTemp(tempData); 

    System.out.format("The average high temperature is %4.1f%n", avgHigh); 
    System.out.format("The average low temperature is %4.1f%n", avgLow); 
    System.out.format("The index of high temperature is %2d%n", indexHigh); 
    System.out.format("The index of low temperature is %2d%n", indexLow); 

    } 

private static void printTempData(double[][] tempData) 
    { 
    System.out.format("%6s:%4s%4s%4s%4s%4s%4s%4s%4s%4s%4s%4s%4s%n","Month","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); 
    System.out.format("%6s:","Low"); 

    for (int i = 0; i < tempData[0].length;i++) 
    { 
    System.out.format("%4.1s", tempData[0][i]); 
    } 
    System.out.format("%n"); 
    System.out.format("%6s: ","High"); 
    for (int i = 0; i < tempData[1].length; i++) 
    { 
    System.out.format("%4.1f", tempData[1][i]); 
    } 
    System.out.format("%n"); 
} 

private static int indexLowTemp(double[][] tempData) 
{ 
    int index = 0; 
    double temp = tempData[0][0]; 

    for (int i = 0; i < tempData[0].length; i++) 
    { 
     if (temp > tempData[0][i]) 
      { 
      temp = tempData[0][i]; 
      index = i; 
      } 
     } 
     return index +1; 
} 

private static int indexHighTemp(double[][] tempData) 
    { 
    int index = 0; 
    double temp = tempData[1][0]; 
    for(int i = 0; i< tempData[1].length; i++) 
     { 
     if (temp < tempData[1][i]) 
      { 
      temp = tempData[1][i]; 
      index = i; 
      } 
     } 

     return index + 1; 
    } 

private static double averageHigh(double[][] tempData) 
    { 
    double avg = 0.0; 
    for(int i=0; i < tempData[0].length; i++) 
     { 
     avg += tempData[0][i]; 
     } 

    avg /= tempData[0].length; 

    return avg; 
    } 

    private static double averageLow(double[][] tempData) 
    { 
    double avg = 0.0; 
    for(int i=0; i > tempData[1].length; i++) 
     { 
     avg += tempData[0][i]; 
     } 

    avg /= tempData[0].length; 

    return avg; 
    } 


private static double getData() 
{ 
    double[][] tempData = new double[2][12]; 
    Random r = new Random(); 

    for (int j = 0; j < tempData[0].length; j++) 
    { 
     tempData[0][j] = 30 + Math.sqrt(j) - r.nextDouble(); 
     tempData[1][j] = 30 + Math.sqrt(j) + r.nextDouble(); 
    } 

    return tempData; 
    } 
} 

Antwort

4

Sie bereits ein Array deklariert

 double[][] tempData = getData(); 

aber Sie sind Versuch

 private static double getData() 

so den Fehler zu nennen

Daher ändern

"Umwandlung double [] [] kann nicht zu verdoppeln umgewandelt werden."
 private static double[][] getData() 
5

Ihre Methode private static double getData() sollte. private static double[][] getData()

0

sollte die Methode private static double[][] getData() sein

Verwandte Themen