반응형
반복문(while)
아래와 같이 사용한다.
int a = 1;
while(a <= 10){
//만약 조건문이 true라면 계속 돌게 된다.
//그래서 while문을 탈출할 수 있는 조건을 주어야 한다.
a++;
}
➰ while문의 사용
public class WhileEx01 {
public static void main(String[] args) {
int a = 1; // 제어 변수 : 반복문의 횟수를 결정할 변수
while(a <= 10) {
System.out.println("hello" + a);
a++; // 제어변수 조작을 통해 반복의 조건식이 언젠가 false되도록 처리.
}
//누적
int i =1;
int sum = 0; // 누적할 변수
while(i <= 10) {
sum += i;
i++;
}
System.out.println("1~10까지 sum: "+sum);
}
}
➰Quiz - while문을 돌려 번호+출석체크를 출력하도록하기 & 구구단 출력하기
import java.util.Scanner;
public class Quiz07 {
public static void main(String[] args) {
/* 맛보기: 출석체크를 출력해보았다.
int i = 1;
while(i <= 20) {
System.out.println(i+"번 학생의 출석을 체크");
i++;
}
*/
/*
* 정수값을 입력받아서
* 입력받은 값에 대한 구구단을 출력
*/
Scanner scan = new Scanner(System.in);
System.out.print("실수를 입력하세요>");
int num = scan.nextInt();
int i = 1;
System.out.println("구구단:"+num+"단");
while(i < 10) {
//System.out.println(num + " X " + i + " = " + num*i);
System.out.printf("%d x %d = %d\n", num, i, num*i);
i++;
}
scan.close();
}
}
➰ while문의 사용 - 100번 회전하는 반복문에서 짝수 출력하기(2가지 방법)
public class WhileEx02 {
public static void main(String[] args) {
//100번 회전하는 반복문에서 짝수만 출력 1
int i = 1;
while(i <= 100) {
if(i%2 ==0) {
System.out.print(i + " ");
}
i++;
}
System.out.println();//줄바꿈 역할
//100번 회전하는 반복문에서 짝수만 출력 2
int j = 2;
while(j<=100) {
System.out.print(j+" ");
j += 2;
}
}
}
➰ while문의 사용 - 1~100까지의 정수중 3의 배수의 갯수 구하기
public class WhileEx03 {
public static void main(String[] args) {
//3의 배수의 개수 구하기
//1~100까지의 정수중
int i = 1;
int count = 0; //개수 체크 변수
while (i <= 100) {
if(i % 3 == 0) { //i는 3의 배수다.
count++;
}
i++;
}
System.out.println("3의 배수 개수는: " + count);
}
}
➰Quiz
아래를 순서대로 실행해볼 것.
1. 1~100까지 정수중에 3의 배수이거나 4의 배수일 경우에 가로로 출력.
2. 1~200까지 정수중에서 6의 배수의 합계
3. 1~100까지 정수 중에서 4의 배수이면서, 8의 배수가 아닌 수의 갯수.
4. 1000의 약수의 개수 (약수는 나누어 떨어지는 수)
public class Quiz08 {
public static void main(String[] args) {
//1. 1~100까지 정수중에 3의 배수이거나 4의 배수일 경우에 가로로 출력.
int i = 1;
System.out.print("1번 결과: ");
while(i <= 100) {
if(i % 3 == 0 || i % 4 == 0) {
System.out.print(i + " ");
}
i++;
}
System.out.println();
//2. 1~200까지 정수중에서 6의 배수의 합계
int sum = 0;
i = 1;
while(i <= 200) {
if(i % 6 == 0) {
sum += i;
}
i++;
}
System.out.println("2번 결과: " + sum);
//3. 1~100까지 정수 중에서 4의 배수이면서, 8의 배수가 아닌 수의 갯수.
int count = 0;
i = 1;
while(i <= 100) {
if(i % 4 == 0 && i % 8 != 0) {
count++;
}
i++;
}
System.out.println("3번 결과: "+ count);
//4. 1000의 약수의 개수 (약수는 나누어 떨어지는 수)
count = 0;
i = 1;
while(i <= 1000) {
if(1000 % i == 0) {
count++;
}
i++;
}
System.out.print("4번 결과 약수의 개수: " + count);
}
}
➰Quiz - 입력된 두 수 사이의 모든 숫자들의 합을 구하기.
import java.util.Scanner;
public class Quiz09 {
public static void main(String[] args) {
//두 수를 입력받습니다.
//두 수 사이의 합을 구하시오.
// 10 13 -> 10+11+12+13
// 13 10 -> 10+11+12+13
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
int b = scan.nextInt();
int sum = 0;
/*
int max = 0, min = 0;
//큰수와 작은 비교
if(a<b) { //1.a~b
min = a;
max = b;
}else { //2. b~a
min = b;
max = a;
}
*/
//더 간결한 버전. 3항연산자를 이용하여!!
int max = a > b ? a : b;
int min = a > b ? b : a;
//순서대로 합을 구함.
while(min <= max) {
sum += min;
min++;
}
System.out.println("두 수의 합은:" + sum);
}
}
💡 오늘 백준 문제 푸는것 buffer 찾아보다가 새로운 단축키를 발견했다. import 한번에 해주는 ctrl+shift+O 넘 간편해서 꿀팁이라고 생각하여 저장~
➰while문의 사용 - 반복문 안에서 scanner로 입력받기.
import java.util.Scanner;
public class WhileEx05 {
public static void main(String[] args) {
/*
* 반복문안에서의 입력
*
* 1개의 정수를 받아서
* 첫번쨰 정수는 반복의 횟수를 결정
*
*/
Scanner scan = new Scanner(System.in);
System.out.print("반복횟수>");
int num = scan.nextInt();
int sum = 0; //누적할 변수
int i = 1;
while(i <= num) {
System.out.print(">");
int a = scan.nextInt();
sum += a; // 입력받은 수의 누적
i++;
}
System.out.println(sum);
scan.close();
}
}반응형
'국비지원 > JAVA' 카테고리의 다른 글
| [JAVA] 5-2. 반복문 for (0) | 2022.09.26 |
|---|---|
| [JAVA] 5-1. 배열과 반복문(while문), do-while문 곁들인.. (0) | 2022.09.26 |
| [JAVA] 4-1. 조건문 switch 활용 (0) | 2022.09.23 |
| [JAVA] 3-3. 조건문 (0) | 2022.09.22 |
| [JAVA] 3-2. 제어문 (0) | 2022.09.22 |