JAVA
자바 조건문과 반복문(5일)
별메아리
2023. 2. 2. 13:31
728x90
여기서는 자바스크립트와 거의 동일하기에 예제로만 다룹니다. 자세한건 자바스크립트 조건문과 반복문을 참조하세요.
if문
package ch04;
public class IfExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
int score = 93;
if(score >= 90) {
System.out.println("점수가 90보다 큽니다.");
System.out.println("등급은 A입니다.");
}
if(score<90)
System.out.println("점수가 90보다 작습니다.");
System.out.println("등급은 B입니다.");
}
}
}
위에 코드는 중괄호블록오류로 등급 a 와 b가 동시에 출력된다.
if-else문
package ch04;
public class ifElseExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
int score = 93;
if(score >= 90) {
System.out.println("점수가 90보다 큽니다.");
System.out.println("등급은 A입니다.");
}
else {
System.out.println("점수가 90보다 작습니다.");
System.out.println("등급은 B입니다.");
}
}
}
if-else if-else문
package ch04;
public class IfElseIfElseExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
int score = 75;
if(score >= 90) {
System.out.println("점수가 90~100입니다.");
System.out.println("등급은 A입니다.");
}
else if (score>=80) {
System.out.println("점수가 80~89입니다..");
System.out.println("등급은 B입니다.");
}
else if (score>=70) {
System.out.println("점수가 70~79입니다..");
System.out.println("등급은 C입니다.");
}
else {
System.out.println("점수가 70 미만입니다..");
System.out.println("등급은 B입니다.");
}
}
}
주사위 번호 뽑기
package ch04;
public class IfDiceExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
int num = (int) (Math.random() * 6) + 1; // 주사위 번호 하나 뽑기
if(num==1) {
System.out.println("1번이 나왔습니다.");
}else if(num==2) {
System.out.println("2번이 나왔습니다.");
}else if(num==3) {
System.out.println("3번이 나왔습니다.");
}else if(num==4) {
System.out.println("4번이 나왔습니다.");
}else if(num==5) {
System.out.println("5번이 나왔습니다.");
}else {
System.out.println("6번이 나왔습니다.");
}
}
}
Math.random() 메서드를사용하여 주사위 눈을 만든다.
int num = (int) (Math.eandom() * n) +start;
switch문
package ch04;
public class SwitchExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
int num = (int) (Math.random() *6) + 1; // 주사위 번호 하나 뽑기
switch(num) {
case 1:
System.out.println("1번이 나왔습니다.");
case 2:
System.out.println("2번이 나왔습니다.");
case 3:
System.out.println("3번이 나왔습니다.");
case 4:
System.out.println("3번이 나왔습니다.");
case 5:
System.out.println("5번이 나왔습니다.");
default:
System.out.println("6번이 나왔습니다.");
}
}
}
break문이 없는 case
package ch04;
public class SwitchNoBreakCaseExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
int time = (int) (Math.random() * 4) + 8;
System.out.println("[현재 시작: "+ time + "시]");
switch(time) {
case 8:
System.out.println("출근합니다.");
case 9:
System.out.println("회의를 합니다.");
case 10:
System.out.println("업무를 봅니다.");
default:
System.out.println("외근을 나갑니다.");
}
}
}
char 타입의 switch문
package ch04;
public class SwitchCharExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
char grade = 'B';
switch(grade) {
case 'A':
case 'a':
System.out.println("우수회원입니다.");
break;
case 'B':
case 'b':
System.out.println("우수회원입니다.");
break;
default:
System.out.println("외근을 나갑니다.");
}
}
}
String 타입의 switch문
package ch04;
public class SwitchStringExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
String position = "과장";
switch(position) {
case "부장":
System.out.println("700만원");
break;
case "과장":
System.out.println("500만원");
break;
default:
System.out.println("300만원");
break;
}
}
}
for문
for ( 초기화식; 조건식; 증감식) {
실행문
}
1부터 10까지 출력
package ch04;
public class ForPrintFrom1TO10Example {
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=1; i<=10; i++) {
System.out.println(i);
}
}
}
1부터 100까지 출력
package ch04;
public class ForSumFrom1to100Exmaple {
public static void main(String[] args) {
// TODO Auto-generated method stub
int sum = 0;
for(int i=1; i<=100; i++) {
sum += i;
}
System.out.println("1~100까지의 합 : " + sum);
}
}
1부터 100까지 합을 출력
package ch04;
public class ForSumfrom1To100Example2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int sum = 0;
int i = 0;
for(i=1; i<=100; i++) {
sum += i;
}
System.out.println("1~" + (i-1) + "합 : " + sum);
}
}
float 타입 카운터 변수
package ch04;
public class ForFloatCounterExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
for(float x=0.1f; x<=1.0f; x+=0.1f) {
System.out.println(x);
}
}
}
0.1은 float 타입으로 정확하게 표현할 수 없으므로 루프 카운터 변수x에 더해지는 값은 0.1 보다 큽니다.
중첩for문
package ch04;
public class ForMulitiplicationTableExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int m=2; m<=9; m++) {
System.out.println("***"+ m + "단 ***");
for (int n=2; n<=9; n++) {
System.out.println(m + "x" + n + "=" + (m*n));
}
}
}
}
while문
1부터 10까지 출력
package ch04;
public class WhileprintFrom1To10Example {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 1;
while (i<=10) {
System.out.println(i);
i++;
}
}
}
1부터 100까지 출력
package ch04;
public class WhileSumFrom1To100Example {
public static void main(String[] args) {
// TODO Auto-generated method stub
int sum = 0;
int i= 1;
while(i<=100) {
sum += i;
i++;
}
System.out.println("1~" + (i-1) + "합 : " + sum);
}
}
break 문
break로 while문 종료
package ch04;
public class BreakExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
while(true) {
int num = (int) (Math.random() * 6 ) + 1;
System.out.println(num);;
if( num == 6 ) {
break;
}
}
System.out.println("프로그램 종료");
}
}
바깥쪽 반복문 종료
package ch04;
public class BreakOutterExample {
public static void main(String[] args) {
Outter: for(char upper = 'A'; upper <= 'Z'; upper++);{
for(char lower = 'a'; lower<='z'; lower++) {
System.out.println(upper + "-" + lower);
if(lower=='g') {
break Outter;
}
}
}
System.out.println("프로그램 실행 종료");
}
}
반복문이 중첩되어 있을 경우 break 문은 가까운 반복문만 종료하고 바깥쪽 반복문은 종료하지 않기 때문에 바깥쪽 반복문이 라벨(이름)을 붙이고 break :이름을 쓰면 종료됩니다.
continue 문
package ch04;
public class ContinueExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=1; 1<=10; i++) {
if(i%2 != 0) {
continue;
}
System.out.println(i); // 홀수는 실행되지 않음
}
}
}
728x90