코린이
자바 낙서장(2) 조건문 배열 본문
반응형
조건문과 반복문
제어문 - 조건문/반복문
제어문 - 조건문/반복문
if(조건){
조건이 ‘참(true)’일 때 실행할 프로그램 코드(들)
}
if(조건){
조건이 ‘참(true)’일 때 실행할 프로그램 코드(들)
} else {
조건이 ‘거짓(false)’일 때 실행할 프로그램 코드(들)
}
if(조건1){
조건1이 ‘참(true)’일 때 실행할 프로그램 코드(들)
} else if(조건2){
조건1이 ‘거짓(false)’이고, 조건2가 ‘참(true)’일 때
실행할 프로그램 코드(들)
} else {
조건1이 ‘거짓(false)’이고, 조건2가 ‘거짓(false)’일 때
실행할 프로그램 코드(들)
}
break;
while을 탈출하여, while문 밖의 첫 번째 코드로 간다.
continue;
while문을 탈출하지 않고, while문의 맨 처음 조건문으로 간다.
while();
// while()문에서 중요한 3가지 초기화 조건 증감 ★★★★★
while(조건){
조건이 ‘참’일 때 실행코드(들)
}
do-while()
do {
실행코드(들);
} while (조건문); 최소한 1번은 실행
while(조건문){
실행코드(들);
} 최소한 0번실행
디버그(debug) - debugging - 프로그램의 오류를 해결하는 것
for문()
for(초기화; 조건문; 증감){
실행코드;
}
배열
배열(Array)
길이 고정(프로그램코드를 직접 수정) 길이가 고정된 같은 타입의 변수들의 모음
배열리스트(ArrayList) 매우 중요★
길이 가변, 다른 타입도 같이 보관 가능
정적으로 사용 시 ex) names
x = names[i]
동적으로 사용 시 ex) ArrayList<String> arNames
x = arNames.get(i)
import java.utill.ArrayList;
ArrayList<타입> 배열리스트명 = new ArrayList<타입>();
ex)
ArrayList<String> names = new ArrayList<String>();
ArrayList<String> alName = new ArrayList<String>();
Scanner sc = new Scanner(System.in);
System.out.print("이름을 입력하세요: ");
String name = sc.nextLine();
while(!name.equals("")) { //enter를 쳐서 끝낸다.
System.out.print("이름을 입력하세요: ");
alName.add(name);
name = sc.nextLine();
}
for(int i = 0; i < alName.size(); i++) {
System.out.println(alName.get(i));//ArrayList에서 값을 출력하려면 .get(i)를 선언
}
System.out.println("인원수: "+alName.size());
index는 0부터 시작(last index == arNames/size()-1)
void outprint(int a , int b);
x = outprint(i, j); //에러
void 반환값없음 (int a , int b); 매개변수
String(int, float 다양함) outprint(int a, int b);
String xl
x = outprint(i, j);
outprint(i, j) 반환값이 있는데 호출 안해도 상관없다.
반응형
Comments