728x90
제어문
//제어문 flow control states
//조건문, 반복문
//조건문(if, switch) : 조건을 만족할때만 {}를 수행(0번 또는 1번)
//자주 사용되는 조건식
// 'A' <= ch && ch <= 'Z' => 문자 ch가 대문자일때
// 'a' <= ch && ch <= 'z' => 문자 ch가 소문자일때
// '0' <= ch && ch <= '9' => 문자 ch가 숫자일때
//str.equals("abc") => 문자열 str의 내용이 "abc"일 때(대소문자 구분함)
//str.equalsIgnoreCase("abc") => 문자열 str의 내용이 "abc"일 때(대소문자 구분안함)
//※블록 {} 여러 문장을 하나로 묶어주는 것
//반복문(for, while) : 조건을 만족하는 동안 {}를 수행(0~n번)
if
int score = 0; //점수를 저장하기 위한 변수
char grade = ' '; //학점을 저장하기 위한 변수. 공백으로 초기화
System.out.print("점수를 입력 : ");
Scanner scanner = new Scanner(System.in);
score = scanner.nextInt();
if(score >=90) {
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else if (score >= 70) {
grade = 'C';
} else {
grade = 'D';
}
System.out.println("학점은 " + grade + "입니다.");
/* ==
int score = 0;
char grade = 'D'; //학점을 D로 초기화
System.out.print("점수를 입력 : ");
Scanner scanner = new Scanner(System.in);
score = scanner.nextInt();
if(score >=90) {
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else if (score >= 70) {
grade = 'C';
}
System.out.println("학점은 " + grade + "입니다.");
else를 없애줄 수 있음
*/
int score = 0;
char grade = ' ', opt = ' ';
System.out.print("점수를 입력 : ");
Scanner scanner = new Scanner(System.in);
score = scanner.nextInt();
if(score >=90) {
grade = 'A';
if(score >= 97) {
opt = '+';
} else if (score>=94) {
opt = '-';
} else {
opt = '0';
}
} else if (score >= 80) {
grade = 'B';
if(score >= 87) {
opt = '+';
} else if (score>=84) {
opt = '-';
} else {
opt = '0';
}
} else if (score >= 70) {
grade = 'C';
if(score >= 77) {
opt = '+';
} else if (score>=74) {
opt = '-';
} else {
opt = '0';
}
} else {
grade = 'D';
}
System.out.println("점수는 " + score + "이고, 학점은 " + grade + opt + "입니다.");
/* ==
int score = 0;
char grade = 'D', opt = '0';
System.out.print("점수를 입력 : ");
Scanner scanner = new Scanner(System.in);
score = scanner.nextInt();
if(score >=90) {
grade = 'A';
if(score >= 97) {
opt = '+';
} else if (score>=94) {
opt = '-';
}
} else if (score >= 80) {
grade = 'B';
if(score >= 87) {
opt = '+';
} else if (score>=84) {
opt = '-';
}
} else if (score >= 70) {
grade = 'C';
if(score >= 77) {
opt = '+';
} else if (score>=74) {
opt = '-';
}
}
System.out.println("점수는 " + score + "이고, 학점은 " + grade + opt + "입니다.");
*/
switch
//switch문
//처리해야 하는 경우의 수가 많을 때 유용한 조건문
//조건식을 계산 => 조건식의 결과와 일치하는 case 문으로 이동 => 이후 문장들을 수행 => break문이나 switch문의 끝을 만나면 switch문 전체를 빠져나감
//조건식이 ture false가 아님
//switch문의 제약조건
//switch문의 조건식 결과는 정수 또는 문자열이어야함
//case문의 값은 정수 상수, 문자열만 가능하며, 중복되지 않아야 함
System.out.print("현재 월을 입력 : ");
Scanner scanner = new Scanner(System.in);
int month = scanner.nextInt();
switch (month) {
case 3: case 4: case 5:
System.out.println("봄입니다.");
break;
case 6: case 7: case 8:
System.out.println("여름입니다.");
break;
case 9: case 10: case 11:
System.out.println("가을입니다.");
break;
default:
System.out.println("겨울입니다.");
break;
}
난수
//임의의 수
//Math.random()
//0.0과 1.0 사이의 임의의 double값을 반환
//0.0 <= Math.randow() < 1.0
double random = Math.random();
System.out.println(Math.round(random*3)+1);
for
//for
//조건을 만족하는 동안 블럭{}을 반복 - 반복횟수를 알 때 적합
//for(①초기화;②조건식;④증감식){
// ③수행될 문장
//}
for(int i = 0; i < 5 ;i++) {
System.out.println("Hello World");
}
System.out.println();
for(int i = 0; i < 5 ;i=i+2) {
System.out.println("Hello World");
}
System.out.println();
for(int i = 1; i < 10 ; i=i*2) {
System.out.println("Hello World");
}
System.out.println();
for(int i = 10; i >=1; i--) {
System.out.println(i);
}
System.out.println();
for(int i = 1, j= 10; i<=10; i++,j--) {
System.out.println("i = " + i + ", j = " + j);
}
중첩 for문
//중첩 for문
for(int i = 2; i<= 9; i++) {
System.out.println(i + "단");
for(int j = 1; j <=9; j++) {
System.out.println(i + " * " + j + " = " + i * j);
}
}
//안쪽 for문이 먼저 실행된 후 바깥for문이 1번 실행
System.out.println();
//별찍기
//**********
//**********
//**********
//**********
//**********
//1.
System.out.println("**********");
System.out.println("**********");
System.out.println("**********");
System.out.println("**********");
System.out.println("**********");
System.out.println();
//2.
for(int i = 1; i <=5 ; i++) {
System.out.println("**********");
}
System.out.println();
//3. 내가 작성한 것
for(int i = 1; i<=5; i++) {
System.out.print("*");
for(int j = 1; j<=9; j++) {
System.out.print("*");
}
System.out.println();
}
System.out.println();
//4.
for(int i = 1; i <=5 ; i++) {
for(int j = 1; j <=10; j++) {
System.out.print("*");
}
System.out.println();
}
System.out.println();
for(int i = 1; i <=10 ; i++) {
for(int j = i; j <=10; j++) {
System.out.print("*");
}
System.out.println();
}
System.out.println();
for(int i = 1; i <=10 ; i++) {
for(int j = 1; j <=i; j++) {
System.out.print("*");
}
System.out.println();
}
while문, do while문
//while문
//조건을 만족시키는 동안 블록 {}을 반복 - 반복횟수를 모를 때 사용
// while(조건식){
// 조건식의 연산결과가 true인 동안, 반복될 문장
//}
int i = 1;
while (i<=10) {
System.out.println(i);
i++;
}
System.out.println();
int sum = 0;
int i1 = 0;
//i를 1씩 증가시켜 sum에 계속 더해나감
while (sum<=100) {
System.out.println(i1 + ", " + sum);
sum += ++i1;
}
//do-while문
//블럭 {}을 최소한 한 번 이상 반복 - 사용자 입력받을 때 유용
//do{
// //조건식의 연산결과가 참일 때 수행될 문장(처음 한 번은 무조건 실행)
//} while(조건식);
int num = 12345, sum = 0;
//10으로 나머지 연산을 하면 마지막 자리를 얻음
for(num = 12345; num > 0; num /= 10) {
System.out.println(num%10);
sum += num%10;
}
//==
// while (num > 0) {
// sum += num %10;
// System.out.println(sum +", " + num%10);
// num = num / 10;
// }
System.out.println("각 자리수의 합 " + sum);
//do while문
int input = 0, answer = 0;
answer = (int)(Math.random()*100)*1; //1~100사이의 임의의 수를 저장
Scanner scanner = new Scanner(System.in);
do {
System.out.println("1과 100 사이의 정수를 입력 : ");
input = scanner.nextInt();
if(input > answer) {
System.out.println("더 작은 수로 시도해보세요");
} else if(input < answer) {
System.out.println("더 큰 수로 다시 시도해보세요");
}
} while (input!=answer);
System.out.println("정답");
break문
//break문
//자신이 포함된 하나의 반복문을 벗어남
int sum = 0;
int i = 0;
while(true) { //무한 반복문 ==> for(;;){}
if(sum > 100)
break; //자신이 속한 하나의 반복문을 벗어남
i++;
sum += i;
}
System.out.println("i = " + i + ", sum = " + sum);
for(;;) {
if(sum>100)
break;
i++;
sum += i;
}
System.out.println("i = " + i + ", sum = " + sum);
continue문
//contunue문
//자신이 포함된 반복문의 끝으로 이동 -> 다음 반복으로 넘어감
//전체 반복 중에서 특정 조건 시 반복을 건너뛸 때 유용
for(int j = 0; j <=10; j++) {
if(j%3==0) {
continue;
}
System.out.print(j + " ");
}
System.out.println();
int menu = 0;
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1. 자바");
System.out.println("2. 자바스크립트");
System.out.println("3. C++");
System.out.print("사용할 수 있는 언어를 선택(1~3)하세요.(종료는 0) : ");
String temp = scanner.nextLine();
menu = Integer.parseInt(temp);
if(menu == 0) {
System.out.println("프로그램을 종료합니다.");
break;
} else if(!(1<=menu && menu <= 3)) {
System.out.println("보기를 잘못 선택했습니다. (종료는 0)");
continue;
}
System.out.println("선택한 언어는 " + menu);
}
반복문에 이름
//이름붙은 반복문
//반복문에 이름을 붙여서 하나 이상의 반복문을 벗어날 수 있음
//for문에 Loop1이라는 이름을 붙임
Loop1 : for(int a=2; a<=9; a++) {
for(int j=1; j<=9; j++) {
if(j==5)
break Loop1;
System.out.println(a + " * " + j + " = " + a*j);
}
System.out.println();
}
System.out.println();
for(int a=2; a<=9; a++) {
for(int j=1; j<=9; j++) {
if(j==5)
break;
System.out.println(a + " * " + j + " = " + a*j);
}
System.out.println();
}
728x90
'organize > 자바' 카테고리의 다른 글
자바 처음부터 다시 시작하기 6 (0) | 2024.04.17 |
---|---|
자바 처음부터 다시 시작하기 5 (0) | 2024.04.15 |
자바 처음부터 다시 시작하기 3 (0) | 2024.04.14 |
자바 처음부터 다시 시작하기 2 (0) | 2024.04.10 |
자바 처음부터 다시 시작하기 1 (0) | 2024.04.10 |