on
#4. 1주차 과제 4 [Chap02_2C-1, 14]
#4. 1주차 과제 4 [Chap02_2C-1, 14]
package practice;
import java.util. Scanner ;
public class PhysicalExamination_2_14 {
//PhysicalExamination클래스 내의 모든 메서드, 클래스에서 공유하는 static 멤버
static final int VMAX = 21 ;
//이름, 키, 시력을 갖는 객체생성
static class PhyscData{
String name;
int height;
double vision;
public PhyscData( String name, int height, double vision) {
this .name = name;
this .height = height;
this .vision = vision;
}
}
//평균키 구하는 메서드
static double aveHeight(PhyscData[]dat) {
double sum = 0 ;
for ( int i = 0 ; i < dat. length ; i + + )
sum + = dat[i].height;
return sum / dat. length ;
}
// 시력 분포 구하는 메서드
static void distVision(PhyscData[]dat, int []dist) {
int i = 0 ;
dist[i] = 0 ;
for (i = 0 ; i < dat. length ; i + + ) {
// 시력이 0.0보다 같거나 높고, 2.1보다 낮거나 같으면
if (dat[i].vision > = 0. 0 & & dat[i].vision < = VMAX / 10. 0 )
//명수를 증가시키는 부분
dist[( int )(dat[i].vision * 10 )] + + ;
}
}
//실행부
public static void main( String [] args) {
Scanner stdIn = new Scanner ( System . in );
PhyscData[]x = {
new PhyscData( "박현규" , 162 , 0. 3 ),
new PhyscData( "함진아" , 173 , 0. 7 ),
new PhyscData( "최윤미" , 175 , 2. 0 ),
new PhyscData( "홍연의" , 171 , 1. 5 ),
new PhyscData( "이수진" , 168 , 0. 4 ),
new PhyscData( "김영준" , 174 , 1. 2 ),
new PhyscData( "박용규" , 169 , 0. 8 ),
};
int []vdist = new int [VMAX];
System . out . println ( "■ 신체검사 리스트 ■" );
System . out . println ( "이름 키 시력" );
System . out . println ( "----------------" );
for ( int i = 0 ; i < x. length ; i + + ) {
System . out .printf( "%-8s%3d%5.1f
" ,
x[i].name, x[i].height, x[i].vision);
}
System . out .printf( "
평균 키: %5.1fcm
" , aveHeight(x));
distVision(x,vdist);
System . out . println ( "
시력 분포" );
for ( int i = 0 ; i < VMAX; i + + )
System . out .printf( "%3.1f~ : %2d명
" , i / 10. 0 ,vdist[i]);
}
}
from http://ej-0929.tistory.com/22 by ccl(S) rewrite - 2021-12-31 06:02:26