Ява - Высшая, Низшая и Средняя - PullRequest
0 голосов
/ 17 мая 2010

Правильно, так почему же в Java появляется эта ошибка:

Исключение в потоке "main" java.lang.Error: Неразрешенная проблема компиляции: Несоответствие типов: невозможно преобразовать значение типа double в int

at rainfall.main (rainfall.java:38)

Из этого:

public class rainfall {

 /**
  * @param args
  */
 public static void main(String[] args) 
 {
 int[]  numgroup;
 numgroup = new int [12];
 ConsoleReader console = new ConsoleReader();
 int highest;
 int lowest;
 int index;
 int tempVal;
 int minMonth;
    int minIndex;
 int maxMonth;
 int maxIndex;


 System.out.println("Welcome to Rainfall");
 // Input (index now 0-based)
 for(index = 0; index < 12; index = index + 1)
 {       
     System.out.println("Please enter the rainfall for month " + index + 1);
     tempVal = console.readInt();
     while (tempVal>100 || tempVal<0)
     {
         System.out.println("The rating must be within 0...100. Try again");
         tempVal = console.readInt();
     }
     numgroup[index] = tempVal;
 }           

 lowest = numgroup[0];
 highest = numgroup[0];
 int total = 0.0;
 // Loop over data (using 1 loop)
 for(index = 0; index < 12; index = index + 1)
 {       
     int curr = numgroup[index];
     if (curr < lowest) {
         lowest = curr;
         minIndex = index;
     }
     if (curr > highest) {
         highest = curr;
         maxIndex = index;
     }
      total += curr;
 }
 float avg = (float)total / numgroup.length;

 System.out.println("The average monthly rainfall was " + avg);
 // +1 to go from 0-based index to 1-based month
 System.out.println("The lowest monthly rainfall was month " + minIndex + 1);
 System.out.println("The highest monthly rainfall was month " + maxIndex + 1);

 System.out.println("Thank you for using Rainfall");

 }


 private static ConsoleReader ConsoleReader() {

  return null;
 }

}

Ответы [ 2 ]

4 голосов
/ 17 мая 2010

Полагаю, виновник этой строки:

int total = 0.0;

должно быть

int total = 0;

вместо.

1 голос
/ 17 мая 2010

Проблема с этой строкой здесь:

int total = 0.0; 

Необходимо изменить общее значение, чтобы оно было типа float

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...