갈림길 이정표

[4. 조건문과 반복문] 조건문 problem 1~5 + α 본문

Programming Language/Java 마스터하기(feat. 이것이 자바다)

[4. 조건문과 반복문] 조건문 problem 1~5 + α

이몽뇽 2020. 7. 19. 22:49
package pack1;

import java.util.Scanner;

public class Test6_probem1 {

	public static void main(String[] args) {
		//문제 1: 키보드로 부터 숫자를 받아 구구단 출력(2~9 단 까지 허용) 
				System.out.println("--problem1--");
				Scanner id = new Scanner(System.in);
				System.out.print("몇 단을 알고 싶으냐?");
				int gugu = id.nextInt();
				
				if(gugu>=2 && gugu<=9) {
					System.out.println("궁금하냐? " + gugu + "단 결과? ");
					System.out.println("옛다");
					for(int count = 1; count<10; count++) {
						System.out.println(gugu + " * " + count + "=" + gugu * count);
					}
				}else {
					System.out.println("너가 계산해");
				}
			System.out.println("프로그램 종료");

	}

}
package pack1;

public class Test6_probem2 {

	public static void main(String[] args) {
		//문제 2: 1~100 사이의 정수 중 3의 배수이면서 5의 배수인 수를 출력하고 그들의 합을 출력
				System.out.println("--problem2--");
				int hab = 0;
				for(int p=1; p<=100; p++) { //1~100사이 정수
					if(p%3==0 && p%5==00) { //3의 배수면서 5의 배수 <AND>
						System.out.print(p + "\t");
						hab += p; //누적기억장소 초기화(초기값=0) , +=: 등차수열
					}
				}
				System.out.println();
				System.out.println("1~100 사이의 정수 중 3의 배수이면서 5의 배수들의 합: " + hab);

	}

}
​
package pack1;

public class Test6_probem3 {

	public static void main(String[] args) {
		//문제 3: 2~9 단까지 출력 (다중 for)
			System.out.println("--problem3--");
//			for(int q=2; q<=9; q++) { //<왜> q>10이건 안되고 q<=9 이건돼?
//				System.out.print(q + " 단은" + "\t\t" +"\n");
//				for(int count = 1; count<10; count++) {
//					System.out.println(q + " * " + count + " = " + q * count + "\t");
//				}
//			}
//			int q=2
//					
//			for(int count = 1; count<10; count++) { //<왜> q>10이건 안되고 q<=9 이건돼?
//				System.out.print(q + " * " + count + " = " + q * count + "\t");
//				for(int q=2; q<=9; q++) {
//					System.out.println(q + " 단은" + "\t\t" +"\n");
//				}
//			}

	}

}

//how to 가로 배열?
package pack1;

public class Test6_problem4 {

	public static void main(String[] args) {
		/*문제 4: *********
		          *******
		           *****
		            ***
		             */
			System.out.println("--problem4--");
			for(int stage = 1; stage<=5; stage++) {
			//줄 하나를 stage변수라 할거임 줄은 1~5줄까지 있음
			//1~5까지 반복할 거임
			//이후 빈칸은 blank, 별은 star 변수로 정의할 거임
				for(int blank = stage-1; blank>=-1; blank--) {
					System.out.print(" ");
				//앞에 비어진 공간 빼고는 첫줄은 (다른줄에 비해) 빈칸이 없음
				//초기값은 stage와 연관시켜야 줄 하나당 다른 값 적용가능
				//조건은 for구문이 참일 때 " "가 실행 되어야 되기 때문에 항상 어떤 수보다는 크거나 같을 때만 적용되게끔 함.
				//증가치는 stage당이 아닌 한 stage 내에서 " "명령이 실행되어져야되기때문에 점점 줄여 나가면 됨
				//※ 그럼 조건문을 어떤수보다 작거나 같다라고 했을 때는 점점 크게 하면 되겠지?
				}
				for(int star = 9-(stage-1)*2; star>=1; star--) {
					System.out.print("*");
				}
				for(int blank = stage-1; blank>=0; blank--) {
					System.out.print(" ");			
				}
				System.out.println();
			}
	}

}
package pack1;

public class Test6_problem5 {

	public static void main(String[] args) {
		//문제5: 문제 4 결과를 뒤집어 출력
		System.out.println("--problem5--");
		for(int stage = 1; stage<=5; stage++) {
			for(int blank = 5-stage; blank>=-1; blank--) {
				System.out.print(" ");			
			}
			for(int star = (stage*2)-1; star>=1; star--) {
				System.out.print("*");
			}
			for(int blank = 5-stage; blank>=1; blank--) {
				System.out.print(" ");			
			}
			System.out.println("");
		}

	}

}
package pack1;

public class Test6_problem_my {

	public static void main(String[] args) {
		int minus = 5;
		int plus = 5;
		for(int i=0; i<5; i++) {
			for(int j=1; j<=9; j++) {
				if(j>= minus && j<=plus) {
					System.out.print("*");
				}else {
					System.out.print(" ");
				}
				
			}
			System.out.println("");
			minus--;
			plus++;
		}
		int m = 2;
		int p = 8;
		for(int i=0; i<5; i++) {
			for(int j=1; j<=9; j++) {
				if(j>= m && j<=p) {
					System.out.print("*");
				}else {
					System.out.print(" ");
				}
				
			}
			System.out.println("");
			m++;
			p--;
		}

	}

}
Comments