코드를 입력하세요 //** 중첩 반복문**
public void ex16() {
//구구단 모두 출력하기
for(int dan = 2; dan <= 9; dan++) { //2~9단까지 차례대로 증가
for(int num = 1; num <= 9; num++) { //각단에 곱해질 수 1~9까지 차례대로 증가
System.out.printf("%2d X %2d =%2d ", dan, num, dan * num);
}
System.out.println(); // 하나의 단 출력이 끝났을 때 줄바꿈
// 아무내용 없는 println()== 줄바꿈
}
}
public void ex17() {
// 구구단 역순 출력
// 9단 ->2단까지 역방향
//곱해지는 수는 1->9 정방향
for(int dan = 9; dan >= 2; dan--) {
for(int num = 1; num <= 9; dan++) {
System.out.printf("%d X %d = %2d ", dan,num, dan*num );
}
System.out.println();
}
}
public void ex18() {
//2중 for 문을 이용해서 다음 모양을 출력하시오
//12345
//12345
//12345
//12345
//12345
for(int i = 1; i <= 5; i++) { //5바퀴 반복하는 for문
for(int j = 1; j <= 5; j++) { //12345 한 줄 출력하는 for문
System.out.println(j);
}
System.out.println();
}
System.out.println("-----------------------------");
//54321
//54321
//54321
for(int i = 1; i <= 3; i--) {
for(int j = 5; j>= 1; j++) {
System.out.println(j);
}
System.out.println();
}
}
public void ex19() {
//2중 for문을 이용하여 다음 모양을 출력하시오.
//1
//12
//123
//1234
for(int x= 1; x<=4; x++) { //줄반복
for(int i = 1; i <= x; i++) {
System.out.print(i);
}
System.out.println();
}
//4321
//321
//21
//1
System.out.println("-------------------------------");
for(int x = 4; x >= 1; x--) {
for(int i = x; i <= 1; i--) {
System.out.print(i);
}
System.out.println();
}
}
public void ex20() {
//숫자세기 count
//1부터 20까지 1씩 증가하면서
//3의 배수의 총 개수 출력
//3의 배수의 합계 출력
//3 6 9 12 18 : 6개
// 3의 배수 합계 : 63
int count = 0; //3의 배수의 개수를 세기 위한 변수
int sum = 0; //3의 배수의 합계를 구하기 위한 변수
for(int i = 1; i<= 20; i++) {
if(i % 3 ==0) {
System.out.print(i + " ");
count++;
sum += i;
}
}
System.out.println(":" + count + "개");
System.out.println("3의 배수 합계 : " + sum);
}
public void ex21() {
//2중 for문과 count를 이용해서 아래모양 출력하기
//1 2 3 4
//5 6 7 8
// 9 10 11 12
int count =1;
for(int x = 1; x <=3; x++) { //3줄
for(int i = 1; i <= 4; i++) { //4칸
System.out.printf("%3d", count);
count++;
}
System.out.println();
}
}
public void ex22() {
}
public void ex23() {
//사용자로부터 입력 받은 숫자의 단부터 9단까지 출력하세요.
//단,2~9 사이가 아닌수를 입력하면 "2~9 사이 숫자만 입력해주세요" 출력하세요
for(int dan = 4; dan <= 9; dan++) {
for(int num = 1; num <= 9; num++) {
System.out.printf("%4d X %4d = %4d", dan,num , dan * num);
}
System.out.println();
}
System.out.println("2~9 사이 숫자만 입력해주세요.");
}
public void ex24() {
Scanner sc = new Scanner(System.in);
System.out.print("정수 입력 : ");
int input = sc.nextInt();
for(int i = 1; i<= 4; i++) {
for(int x = 1; x<=i; x++) {
System.out.print("*");
}
System.out.println("");
}
}
public void ex25() {
Scanner sc = new Scanner(System.in);
System.out.print("정수 입력 : ");
int input = sc.nextInt();
for(int i =4; i>=1; i--) {
for(int x = i; x<=i; x--) {
System.out.print("*");
}
System.out.println("");
}
}
}
유익한 글 잘 봤습니다, 감사합니다.