2014-12-09 5 views
-2

메읶 클래스로 값을 되돌리려는 코드를 만들려고하는데, 그 답이 0.0으로 돌아 왔을 때 왜 그랬고 어떻게 고치겠습니까?메쏘드에서 값 반환하기

BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
String[]temp = new String[7]; 
int[] arr = new int[7]; 
for (int i = 0; i < 7; i++) { 
    System.out.println("Please enter the temperature for the " + (i + 1)+" day of the week"); 
    //Not the most gramatically correct. But i did what i could while using the loop. 
    temp[i] = br.readLine(); 
} 

System.out.println("The temperature for Monday is: " + temp[0]); 
System.out.println("The temperature for Tuesday is: " + temp[1]); 
System.out.println("The temperature for Wednesday is: " + temp[2]); 
System.out.println("The temperature for Thursday is: " + temp[3]); 
System.out.println("The temperature for Friday is: " + temp[4]); 
System.out.println("The temperature for Saturday is: " + temp[5]); 
System.out.println("The temperature for Sunday is: " + temp[6]); 

double avg = averageValue(arr); 
System.out.println("Avg Temp for the week is: \t\t " + avg); 

public static double averageValue(int[] arr) { 
    double average = 0; 
    for (int i = 0; i< arr.length; i++) { 
     average += arr[i] 
    } 
    return average/arr.length; 
} 
+0

편곡은 코드에서 초기화되지 않습니다. – Rndm

+0

내 배열 그대로 임시로 전환 할 수 있습니까? – dappers

답변

0

다음 코드는이를 수정 한 것 같습니다. arr 객체에 값을 넣어야했습니다.

public static void main(String[] args) { 
    try { 
     BufferedReader br = new BufferedReader 
       (new InputStreamReader(System.in)); 

     String[] temp = new String[7]; 
     int[] arr = new int[7]; 
     for (int i = 0; i < 7; i++) { 
      System.out.println("Please enter the temperature for the " + (i + 1) + " day of the week"); //Not the most gramatically correct. But i did what i could while using the loop. 
      temp[i] = br.readLine(); 
      arr[i] = Integer.parseInt(temp[i]); // ** 
     } 

     System.out.println("The temperature for Monday is: " + temp[0]); 
     System.out.println("The temperature for Tuesday is: " + temp[1]); 
     System.out.println("The temperature for Wednesday is: " + temp[2]); 
     System.out.println("The temperature for Thursday is: " + temp[3]); 
     System.out.println("The temperature for Friday is: " + temp[4]); 
     System.out.println("The temperature for Saturday is: " + temp[5]); 
     System.out.println("The temperature for Sunday is: " + temp[6]); 

     double avg = averageValue(arr); 
     System.out.println("Avg Temp for the week is: \t\t " + avg); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

public static double averageValue(int[] arr) { 
    double average = 0; 
    for (int i = 0; i < arr.length; i++) { 
     average += arr[i]; 
    } 
    return average/arr.length; 
} 

출력

Please enter the temperature for the 1 day of the week 
1 
Please enter the temperature for the 2 day of the week 
2 
Please enter the temperature for the 3 day of the week 
3 
Please enter the temperature for the 4 day of the week 
6 
Please enter the temperature for the 5 day of the week 
7 
Please enter the temperature for the 6 day of the week 
8 
Please enter the temperature for the 7 day of the week 
9 
The temperature for Monday is: 1 
The temperature for Tuesday is: 2 
The temperature for Wednesday is: 3 
The temperature for Thursday is: 6 
The temperature for Friday is: 7 
The temperature for Saturday is: 8 
The temperature for Sunday is: 9 
Avg Temp for the week is:  5.142857142857143 
0

당신은 String[] temp 배열의 온도에 대한 사용자의 입력을 받고 있지만 평균을 계산하는 동안 당신은 어떤 값이없는 배열 int[] arr을 전달하고 있습니다. 그러므로 그것은 0이됩니다.

br.readLine()의 값은 String 형식입니다. 평균을 계산하려면 temp[]의 모든 값을 정수로 변환하고 메소드를 호출하기 전에 arr[]에 저장해야합니다.

대신 스캐너를 사용하여 int 값을 직접 입력 할 수 있습니다. 힌트 :

Scanner sc = new Scanner(System.in); 
arr[0] = sc.nextInt();