Created
December 17, 2016 17:56
-
-
Save JoeUnsung/821aa8c12317e50ce5ffd6d487b13f39 to your computer and use it in GitHub Desktop.
calculate average
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.text.*; | |
class Main { | |
public static double avgCnt(int[] list, int cnt){ | |
double sum = 0; | |
for(int i = 0 ; i < cnt ; i++){ | |
sum += (double)list[i]; | |
} | |
return sum/cnt; | |
} | |
public static void main(String[] args) { | |
int numCnt = 0; | |
Scanner sc = new Scanner(System.in); | |
while(true){ | |
numCnt = sc.nextInt(); | |
if((numCnt >= 1) && (numCnt <= 1000)){ | |
break; | |
} | |
System.out.println("다시 입력하세요."); | |
} | |
int[] numReposi = new int[numCnt]; | |
for (int i = 0 ; i < numReposi.length ; i++){ | |
while(true){ | |
numReposi[i] = sc.nextInt(); | |
if(numReposi[i] > 100){ | |
System.out.println("다시 시도하세요."); | |
continue; | |
} | |
else{ | |
break; | |
} | |
} | |
} | |
System.out.println(); | |
System.out.print("출력값 : "); | |
System.out.format("%.0f%n", avgCnt(numReposi, numCnt)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment