반응형
숙제.
1.20번째의 숫자부터 평균을 구하시오.
조건절의 조건에서 count가 20보다 크거나 같다면
입력받은 값을 총합에 더하고, 카운트 2 개수가 1씩 증가해서
총합/카운트를 하면 평균이 나옴
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class TwentyProgram {
//파일에 6개의 정수를 저장했다.
public static void main(String[] args) throws IOException {
FileInputStream fis =new FileInputStream("res/ex07data.txt");
Scanner scan = new Scanner(fis);
String num;
float avg ;
int count = 0;
int total = 0;
int count2 = 0;
while(scan.hasNext()){
int n ;
num = scan.next();
n= Integer.parseInt(num);
count ++;
if( count >= 20){
total +=n;
count2 ++;
}
}
scan.close();
fis.close();
avg = total/(float)count2;
System.out.printf("짝수 번째의 합은 %d \n",total);
System.out.printf("count is %d\n" ,count2);
System.out.printf("평균은 %f" ,avg);
}
}
2. 가장 큰 수를 구하여라
max라는 변수 선언후 초기값을 0으로 지정
조건식에서 max가 입력받은 값보다 작다면
max에 입력받은 값을 대입
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class MaxProgram {
//파일에 6개의 정수를 저장했다.
public static void main(String[] args) throws IOException {
FileInputStream fis =new FileInputStream("res/ex07data.txt");
Scanner scan = new Scanner(fis);
String num;
int max =0;
int a;
while(scan.hasNext()){
num = scan.next();
a=Integer.parseInt(num);
if( max < a) {
max = a;
}
}
scan.close();
fis.close();
System.out.printf("가장 큰값은 : %d" ,max);
}
}
3. 가장 작은 수를 구하여라
min라는 변수를 선언후 초기화값을 9999로 설정
조건식에서 min보다 입력값이 작다면 min에 값을 대입
다음엔 대입된값보다 입력값이 작다면 min에 대입 무한반복
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class MinProgram {
//파일에 6개의 정수를 저장했다.
public static void main(String[] args) throws IOException {
FileInputStream fis =new FileInputStream("res/ex07data.txt");
Scanner scan = new Scanner(fis);
String num;
int a;
int min = 9999;
while(scan.hasNext()){
num = scan.next();
a=Integer.parseInt(num);
if( min > a ) {
min = a;
}
}
scan.close();
fis.close();
System.out.printf("가장 작은값은 : %d" ,min);
}
}
반응형
'웹 프로그래밍 기초 > 자바기반의 웹&앱 응용SW 개발자' 카테고리의 다른 글
자바기반의 웹&앱 응용 SW개발자 양성과정 5일차 -14 (0) | 2020.02.25 |
---|---|
자바기반의 웹&앱 응용 SW개발자 양성과정 5일차 -13 (0) | 2020.02.25 |
자바기반의 웹&앱 응용 SW개발자 양성과정 4일차-11 (0) | 2020.02.24 |
에이콘 아카데미 JAVA 복습 1주차 정리 -2 (0) | 2020.02.23 |
에이콘 아카데미 JAVA 복습 1주차 정리 (0) | 2020.02.22 |
댓글