#예제1
|
다음 모양 출력하기
1234 1234 1234 |
[기능]
package loop.ex;
import java.util.Scanner;
public class LoopEx2 {
// 필드 (전역 변수 비슷)
Scanner sc = new Scanner(System.in);
public void method1() {
for(int i = 1 ; i <= 3 ; i++) { // 행 제어 (3행 반복)
for (int y = 1 ; y <=4 ; y++) { // 열 제어 (4열 반복)
System.out.print(y);
}
System.out.println(); // 한 행이 끝나면 줄바꿈
}
}
[실행]
package loop.run;
import loop.ex.LoopEx2;
public class LoopRun {
public static void main(String[] args) {
LoopEx2 ex2 = new LoopEx2();
ex2.method1();
#예제2
| 다음 모양 출력하기 54321 54321 54321 54321 |
[기능]
public void method2() {
for(int i = 1 ; i <= 4 ; i++) { // 행 제어(4행 반복)
for(int y = 5 ; y >= 1 ; y--) { // 열 제어(5열 반복)
System.out.print(y);
}
System.out.println(); // 한 행이 끝나면 줄 바꿈
}
}
[실행]
package loop.run;
import loop.ex.LoopEx2;
public class LoopRun {
public static void main(String[] args) {
LoopEx2 ex2 = new LoopEx2();
ex2.method2();
#예제3
| 행, 열, 정방향(1)/역방향(-1) 입력 받아 출력 행 입력 : 3 열 입력 : 6 정방향(1)/역방향(-1) : 0 654321 654321 654321 |
[기능] 방법 1
public void method3(){
System.out.println("행 입력 : ");
int row = sc.nextInt();
System.out.println("열 입력 : ");
int col = sc.nextInt();
System.out.print("정방향(1)/역방향(-1) : ");
int direction = sc.nextInt();
for(int x= 1 ; x <= row ; x++) { // 행
if(direction == 1) {
for(int y = 1 ; y <= col ; y++) {
System.out.print(y); // 정방향
}
}else {
for(int y = col ; y >= 1 ; y--) {
System.out.println(y);
}
}
System.out.println(); // 행출력 완료 -> 줄바꿈
}
[기능] 방법 2
int start = 1;
int end = col;
if(direction == -1) { // 역 방향
start = col;
end = 1;
for(int x = 1 ; x <= row ; x++) {
for(int y = start ; ; y += direction) {
if(direction == 1 && y <= end) {
System.out.print(y);
continue;
}
if (direction == -1 && y >= end) {
System.out.print(y);
continue;
}
break;
}
System.out.println(); // 줄바꿈
}
[실행]
package loop.run;
import loop.ex.LoopEx2;
public class LoopRun {
public static void main(String[] args) {
LoopEx2 ex2 = new LoopEx2();
ex2.method3();
#예제4
|
다음 모양 출력 하기
(0,0) (0,1) (0,2) (1,0) (1,1) (1,2) (2,0) (2,1) (2,2) |
[기능]
public void method4() {
for(int row = 0 ; row < 3 ; row++) { // 0~2행(1번칸)
for(int col = 0 ; col < 3 ; col++ ) { // 0~2열(2번칸)
System.out.printf("(%d,%d) ", row, col);
}
System.out.println(); // 줄바꿈
}
}
[실행]
package loop.run;
import loop.ex.LoopEx2;
public class LoopRun {
public static void main(String[] args) {
LoopEx2 ex2 = new LoopEx2();
ex2.method4();
#예제5
|
다음 모양 출력 하기
(0,0) (1,0) (1,1) (2,0) (2,1) (2,2) |
[기능]
public void method5() {
for(int row = 0 ; row < 3 ; row++) {
for(int col = 0 ; col <= row ; col++) {
System.out.printf("(%d,%d) ", row, col);
}
System.out.println(); // 줄바꿈
}
}
[실행]
package loop.run;
import loop.ex.LoopEx2;
public class LoopRun {
public static void main(String[] args) {
LoopEx2 ex2 = new LoopEx2();
ex2.method5();
#예제6
|
다음 모양 출력 하기
(0,0) (0,1) (0,2) (1,0) (1,1) (2,0) |
[기능]
public void method6() {
for(int row = 0 ; row < 3 ; row++) {
for(int col = 0 ; col < 3 - row ; col++) {
System.out.printf("(%d,%d) ", row, col);
}
System.out.println(); // 줄바꿈
}
}
[실행]
package loop.run;
import loop.ex.LoopEx2;
public class LoopRun {
public static void main(String[] args) {
LoopEx2 ex2 = new LoopEx2();
ex2.method6();
#예제7
| 0이 입력 될 때 까지 입력된 모든 수의 합 |
[기능]
public void method7() {
int input = 0; // 숫자를 입력 받을 변수
int sum = 0; // 합계
do {
System.out.print("숫자 입력 : ");
input = sc.nextInt();
sum += input;
} while(input != 0) ;
System.out.println("합계 : " + sum);
}
[실행]
package loop.run;
import loop.ex.LoopEx2;
public class LoopRun {
public static void main(String[] args) {
LoopEx2 ex2 = new LoopEx2();
ex2.method7();
#결과

'BackEnd > Java' 카테고리의 다른 글
| [Java]배열(Array) for문 (1) | 2024.07.21 |
|---|---|
| [Java]배열(Array) (0) | 2024.07.21 |
| [Java]Control 조건문(Loop1) (0) | 2024.07.21 |
| [Java]Control 조건문(Condition) (0) | 2024.07.21 |
| [Java]Operator (0) | 2024.07.21 |