1) 선택문 : if-else문, switch문
2) 반복문 : while문, for문, do-while문
3) 분기문 : break문, continue문
break문은 선택문, 반복문을 벗어난다.
import java.util.Scanner;
// 예제) 평균 계산
public class Test001 {
public static void main(String[] args) {
int total = 0;
int count = 0;
int grade;
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print("점수 입력: ");
grade = sc.nextInt();
if (grade < 0)
break;
total += grade;
count++;
}
System.out.println("평균은 "+ total / count);
}
}
// continue문 예제
public class Test001 {
public static void main(String[] args) {
String s = "no news is good news";
int n = 0;
int i;
for (i=1; i<=s.length(); i++) {
if(s.charAt(i) != 'n') // if(s.charAt(i) == 'n') 이게 훨신 효율적이다.
continue; // n++;
n++;
}
System.out.println("n의 개수: "+ n);
}
}
댓글 없음:
댓글 쓰기