Skip to content

Instantly share code, notes, and snippets.

@JoeUnsung
Created December 17, 2016 17:56
Show Gist options
  • Save JoeUnsung/821aa8c12317e50ce5ffd6d487b13f39 to your computer and use it in GitHub Desktop.
Save JoeUnsung/821aa8c12317e50ce5ffd6d487b13f39 to your computer and use it in GitHub Desktop.
calculate average
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