package ex2.control;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;
public class Program {
public static void main(String[] args) throws FileNotFoundException {
// 기본형식 : primitive type
int kor, eng;
int math;
int total;
float avg;
kor = 0;
eng = 0;
math = 0;
while(true)
{
// 여기에 성적을 입력하는 코드를 작성하시오.
Scanner sc = new Scanner(System.in); // 반복에 포함될 필요없음
System.out.println("┌────────────────────┐");
System.out.println("│ 성적 입력 │");
System.out.println("└────────────────────┘");
System.out.printf("국어 : ");
kor = Integer.parseInt(sc.nextLine());
System.out.printf("영어 : ");
eng = Integer.parseInt(sc.nextLine());
System.out.printf("수학 : ");
math = Integer.parseInt(sc.nextLine());
total = kor + eng + math;
avg = total / 3.0f;
System.out.println("┌────────────────────┐");
System.out.println("│ 성적 출력 │");
System.out.println("└────────────────────┘");
System.out.printf("국어 : %d\n", kor);
System.out.printf("영어 : %d\n", eng);
System.out.printf("수학 : %d\n", math);
System.out.printf("총점 : %2d\n", total);
System.out.printf("평균 : %26.2f\n", avg);
}
}
}
while(true){ while의 종속된 문장 } : 무한반복한다. while의 조건이 참인동안 실행된다. 실행 후 다시 시작점으로돌아간다.
반복을 끝내고싶을때 : 강제종료
while(조건)이 거짓이된다면 while문을 벗어날 조건이된다.
즉, (조건)에 변수를 넣어 선택구조를 만들 수 있다.
import java.util.Scanner;
public class Mar5th {
public static void main(String[] args) {
int score =0;
Scanner scan = new Scanner(System.in);
boolean more = true;
while(more) {
System.out.print("What grade did you get?");
score = scan.nextInt();
System.out.println("Let me know your score. It's "+score);
System.out.print("Did you wanna try again?");
more = scan.nextBoolean();
}
System.out.println("Bye");
}
}
boolean more = true
boolean : 논리자료형, 참과 거짓을 의미
more = true : 변수 more 를 true초기화한다.
while(more) { }
while(more) : more가 true를 의미하기때문에 무한반복할것이다.
more = sc.nextBoolean();
sc을 통해 nextboolean메소드를 호출한다.
nextBoolean() : true 또는 false를 입력하면 그 문자열을 boolean값으로 바꾸어준다.
Unreachable 에러 발생
while(true) { 실행문 }
다른실행문;
while이 무한반복하기 때문에 while{ }뒤에 어느문장도 실행될 수없어 에러가 발생한다.
FileInputStream, FileOutputStream 을 이용해 파일복사 & 반복문
반복문을 통해 bmp파일의 바이트를 순서대로 입력하고, 출력하기
A라는 파일가져와서 B라는 파일을 만들어보자.
package ex2.control;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyProgram {
public static void main(String[] args) throws IOException {
// 1.res/img.bmp 파일을 읽기 위한 FileInputStream 객체 fis를 생성하고
//res/img_copy.bmp 파일로 쓰기 위한 FileOutputStream 객체 fos를 생성한다.
FileInputStream fis = new FileInputStream("res/aaa.bmp"); //이미지파일 불러오기
FileOutputStream fos = new FileOutputStream("res/img_copy.bmp"); //이미지파일출력
// 2. fis를 통해서 1 바이트를 읽어서 변수 b에 담는다.
// b의 값을 fos로 통해서 출력한다.
int b = 0;
while((b = fis.read()) != -1)
fos.write(b);
//3. 2번을 더 이상 읽어들인 값이 없을때까지 반복
fis.close();
fos.close();
System.out.println("복사완료");
}
}
FileInputStream fis = new FileInputStream("bmp파일이 저장된 상대경로");
FileInputStream: 입력스트림을 이용해 파일경로를 불러온다.
fis : 연산자 new를 통해 객체 fis 생성
FileOutputStream fos = new FileOutputStream("복사본 저장할 상대경로");