컴퓨터프로그래밍/JAVA

12_배열예제(1등구하기)

zelkova 2013. 11. 4. 18:34

<목차로 돌아가기>



1.틀 만들기

public class firstsearch

{

public Main()

{


}


public static void main(String[] args)

{


}

}


2.변수 선언하기

public class firstsearch

{

public firstsearch()

{


}


public static void main(String[] args)

{

int[] english ={48,12,89,65,43};

int[] math = {74,21,70,48,38};

int[] science ={90,79,69,59,49};


int total[] = new int[5];

float average[]= {0.0f,0.0f,0.0f,0.0f,0.0f};

int index;

   }

}


3.합과 평균 구하기

public class firstsearch

{

public firstsearch()

{


}


public static void main(String[] args)

{

int[] english ={48,12,89,65,43};

int[] math = {74,21,70,48,38};

int[] science ={90,79,69,59,49};


int total[] = new int[5];

float average[]= {0.0f,0.0f,0.0f,0.0f,0.0f};

int index;


for(int i=0;i<5;i++)

{

total[i] = english[i] + math[i] + science[i];

average[i] = total[i]/(float)3.0;//정수형을 실수형으로 바꾸어 나누어 줍니다.

}

}

}


4.최고 득점자 구하기

public class firstsearch

{

public firstsearch()

{


}


public static void main(String[] args)

{

int[] english ={48,12,89,65,43};

int[] math = {74,21,70,48,38};

int[] science ={90,79,69,59,49};


int total[] = new int[5];

float average[]= {0.0f,0.0f,0.0f,0.0f,0.0f};

int index;


for(int i=0;i<5;i++)

{

total[i] = english[i] + math[i] + science[i];

average[i] = total[i]/(float)3.0;//정수형을 실수형으로 바꾸어 나누어 줍니다.

}

index=0;

for(int i=0;i<5;i++)

{

if(total[index]<total[i])

{

index=i;

}

}

}

}


5. 최고 득점자 출력하기

public class firstsearch

{

public firstsearch()

{


}


public static void main(String[] args)

{

int[] english ={48,12,89,65,43};

int[] math = {74,21,70,48,38};

int[] science ={90,79,69,59,49};


int total[] = new int[5];

float average[]= {0.0f,0.0f,0.0f,0.0f,0.0f};

int index;


for(int i=0;i<5;i++)

{

total[i] = english[i] + math[i] + science[i];

average[i] = total[i]/(float)3.0;//정수형을 실수형으로 바꾸어 나누어 줍니다.

}

index=0;

for(int i=0;i<5;i++)

{

if(total[index]<total[i])

{

index=i;

}

}

System.out.println("번호 / 총점 / 평균");

System.out.println("------------------");

for(int i=0;i<5;i++)

{

System.out.println(i+"/"+total[i]+"/"+average[i]);

System.out.println("------------------");

System.out.println("최고득점자 번호: " + index);

}

}

}


반응형

'컴퓨터프로그래밍 > JAVA' 카테고리의 다른 글

패키지  (0) 2016.04.16
한글깨짐 방지  (0) 2014.06.12
JAVA - 스레드  (0) 2013.10.16
JAVA - 컬렉션  (0) 2013.10.15
JAVA - Scanner클래스  (0) 2013.10.14