continue : filtering
(조건을 만족하면 pass됨, 필터링 할 조건을 걸어주면 됨.)
while 문에서 continue를 실행 시킬 시, if 문 안에도 i++ 가 있어야 함.
단축키
alt + z : 긴 문장 여러줄로 나오게 하기
ctrl + a : 전체 선택
Caution
File Read 시 여러개의 폴더를 열어놓고 작업하는 경우, 파일의 정확한 경로를 찾지 못 할 수 있음
파일 명 앞에 파일의 위치 + / 입력.
File file = new file("파일 위치 / 파일 이름"); (절대 위치 입력)
Lorem : 임의의 문자 생성
Lorem 10000 : 임의의 문자 10000개 생성
<배열>
타입[ ] 배열이름 = new 타입 [생성개수]
int [ ] array = new int [5]
배열변수는 배열이 생성된 곳의 메모리 값을 저장하는 참조형 변수이다.
배열 생성 시 배열 안의 값은 기본적으로 그 타입의 기본값이 들어가 있음.
•배열 생성과 동시에 값 설정 가능
int [ ] arr2 = { 10, 20, 30};
s.nextInt 후에 s.nextLine 이 나오는 경우, s.nextLine 한번 더 입력해야하는 이유 :
숫자 + 엔터 키를 보통 입력하는데 nextInt는 숫자키만 가져가서 엔터키 입력이 남아있다
그래서 다음 nextLine 이 엔터키를 가져가는 현상이 발생.
그것을 막기위해서 nextLine을 한번 더 입력해 주어야 함.
모든 정수형 데이터 입력 후 동일.
continue : filtering (조건을 만족하면 pass됨, 필터링 할 조건을 걸어주면 됨.)
while 문에서 continue를 실행 시킬 시, if 문 안에도 i++ 가 있어야 함.
alt + z : 긴 문장 여러줄로 나오게 함
ctrl + a : 전체 선택
File Read 시 여러개의 폴더를 열어놓고 작업하는 경우, 파일의 정확한 경로를 찾지 못 할 수 있음
파일 명 앞에 파일의 위치+ / 입력.
File file = new file("파일 위치 / 파일 이름"); (절대 위치 입력)
•html파일에서
Lorem : 임의의 문자 생성
Lorem 10000 : 임의의 문자 10000개 생성
<배열>
타입[ ] 배열이름 = new 타입 [생셩개수]
int [ ] array = new int [5]
배열변수는 배열이 생성된 곳의 메모리 값을 저장하는 참조형 변수이다.
배열 생성 시 안의 값은 기본적으로 그 타입의 기본값이 들어가 있음.
•배열 생성과 동시에 값 설정 가능
int [ ] arr2 = { 10, 20, 30};
s.nextInt 후에
s.nextLine 이 나오려면
s.nextLine 한번 더 입력해야함
숫자 + 엔터 키를 보통 입력하는데 nextInt는 숫자키만 가져가서 엔터키 입력이 남아있다
그래서 다음 nextLine 이 엔터키를 가져가는 현상이 발생
그것을 막기위해서 nextLine을 한번 더 입력해 주어야 함.
모든 정수형 데이터 입력 후 동일.
•Nested Array : 행렬과 비슷
Code
public class ArrayEx {
public static void main(String[] args) {
int[] arr1 = new int[3];
arr1[0] = 90;
arr1[1] = 80;
arr1[2] = 70;
int [] arr2 = {10, 20, 30};
// arr3 = {10, 20, 30} 불가능
// arr3 = new int []{10, 20, 30};
int arr4[] = new int[3]; // 옛날 방식
int size = 10;
// 배열의 크기는 변수로 설정 가능 (같은 타입)
int[] arr5 = new int[size];
}
}
Array를 이용한 간단한 점수관리 시스템
import java.util.Scanner;
public class ArrayEx04 {
public static void main(String[] args) {
// int[] score = {95, 85, 80, 70, 92};
// String [] name = {"송종하", "정시안", "손경은", "송효빈", "안해일"};
Scanner s = new Scanner(System.in);
System.out.print("생성할 데이터 수 : ");
int len = s.nextInt();
int[] score = new int[len];
String[] name = new String[len];
int next_index = 0;
while(true) {
int sel = 0;
System.out.print("1.점수추가, 2.점수조회, 3.점수수정, 0.종료 : ");
sel = s.nextInt();
s.nextLine(); // 엔터키 입력을 비우기 위해 추가함
switch(sel) {
case 1:
if (next_index > len-1) {
System.out.println("더 이상 입력할 수 없습니다.");
}
else {
System.out.print("이름 : ");
String stu_name = s.nextLine();
System.out.print("점수 : ");
int stu_score = s.nextInt();
s.nextLine(); // 엔터키 입력을 비우기 위해 추가함
name[next_index]=stu_name;
score[next_index]=stu_score;
System.out.println("점수가 저장되었습니다.");
next_index++;
break;
}
case 2:
System.out.print("조회할 학생의 번호 (0-" + (len-1) + ") -1 to quit : ");
int n = s.nextInt();
if (n >= 0 && n < len) {
System.out.println("이름 : " + name [n]);
System.out.println("점수 : " + score [n] + "점");
}
else {
System.out.println("잘못된 번호입니다. [번호범위 : 0-" + (len-1) + "]");
}
break;
case 3:
System.out.print("수정할 학생의 번호 (0-" + (len-1) + ") : " );
int md = s.nextInt();
s.nextLine();
System.out.println("== 기존 데이터 =====");
System.out.println("이름 : " + name [md]);
System.out.println("점수 : " + score [md] + "점");
System.out.println("== 수정 데이터 =====");
System.out.print("이름 : ");
String mod_name = s.nextLine();
System.out.print("점수 : ");
int mod_score = s.nextInt();
s.nextLine();
System.out.println("이름 : " + name [md] + " -> " + mod_name);
System.out.println("점수 : " + score [md] + " -> " + mod_score);
System.out.print("수정하시겠습니까? (1:예 / 2:아니요) : ");
int ans = s.nextInt();
s.nextLine();
if (ans == 1) {
name[md]=mod_name;
score[md]=mod_score;
System.out.println("수정했습니다.");
}
else if (ans == 2) {
break;
}
else {
System.out.println("잘못된 입력입니다.");
}
break;
case 0:
System.out.println("종료합니다.");
s.close();
return;
default :
System.out.println("잘못된 메뉴 번호입니다.");
}
}
}
}
import java.util.Scanner;
public class ArrayLoopEx04 {
public static void main(String[] args) {
int [] covid = {32451, 29581, 25434, 13296, 35177, 31352, 28130};
int sum = 0;
double avg = 0;
for(int i=0; i<covid.length; i++) {
sum = sum + covid[i];
}
avg = sum/(double)covid.length;
Scanner s = new Scanner(System.in);
System.out.print("감염자 수를 입력하세요 : ");
int infect = s.nextInt(); // double로 입력해도 됨.
// System.out.print("코로나 감염 주의 단계 : " + (infect >= avg ? "위험" : "주의"));
System.out.print("코로나 감염 주의 단계 : ");
if (infect >= avg) {
System.out.println("위험" );
}
else {
System.out.println("주의" );
}
s.close();
}
}
import java.util.Scanner;
public class DoWhileEx01 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int sum = 0, cnt = 0, input = 0;
do {
System.out.print("입력 (to quit 0) : ");
input = s.nextInt();
sum = sum + input;
cnt++;
}
while (input !=0);
System.out.println("합계 : " + sum + " / 평균 : " + sum/(double)cnt);
s.close();
}
}
html에서 lorem을 통해 생성한 문장을 txt파일로 복붙해서 테스트
import java.io.File;
import java.io.FileReader;
public class FileReadEx {
public static void main(String[] args) throws Exception { // error 발생해도 처리하지 않음.
// 그대로 console에 출력.
File file = new File("test.txt");
FileReader reader = new FileReader(file);
while(true) {
int rd = reader.read(); // 한 글자씩 읽기
if (rd == -1) { // 읽은 결과 -1이면, 파일의 끝에 도달한 것
break;
}
System.out.print((char)rd);
}
reader.close();
}
}