<공포의 별찍기>

//똑같은 코드입니다 퍼가실수 있게 해놓았습니다.

package com.day4;

public class Test1 {

	public static void main(String[] args) {

		int i,j;
		
/*
		for(i=1;i<=5;i++){
			
			for(j=1;j<=5-i;j++){
				
				System.out.print(" ");//공백한칸				
								
			}
				
			for(j=1;j<=i;j++){
				
				System.out.print("*");
			}
			
			System.out.println();//줄바꿈
			
		}
		
	}

*/

/*
 		for(i=1;i<=5;i++){
			
			for(j=1;j<=5-i;j++){
				
				System.out.print(" ");//공백한칸				
								
			}
				
			for(j=1;j<=i*2-1;j++){
				
				System.out.print("*");
			}
			
			System.out.println();//줄바꿈
			
		}
		
	}
	*/
/*		
 		for(i=5;i>=1;i--){
			         //i=i-1
 			
			for(j=1;j<=5-i;j++){
				
				System.out.print(" ");//공백한칸				
								
			}
				
			for(j=1;j<=i*2-1;j++){
				
				System.out.print("*");
			}
			
			System.out.println();//줄바꿈
			
		}
		
	}
} // 영역은 중괄호를 더블클릭하면 알 수 있음
		*/
		
		for(i=5;i>=1;i--){
			
			for(j=1;j<=5-i;j++){
				
				System.out.print(" ");
				
				}
			
			for(j=1;j<=i * 2 -1;j++){ // j(10)<=9
				
				System.out.print("*");
				
			}
			
			System.out.println();
			
		}
		
		for(i=2;i<=5;i++){
			
			for(j=1;j<=5-i;j++){
				
				System.out.print(" ");
				
			}
			
			for(j=1;j<=i * 2 -1;j++){
				
				System.out.print("*");
				
			}
			
			System.out.println();
			
		}
		
	}
}

 

==Console==

==Console==

 

==Console==

 

<계산기>

==Console==

<배열>

 

==추가 설명 ==

// 명령어 ~readLine(): ()로 사용해야 함. // 배열의 length만 ()를 사용하지 않음.

==Console==

 

<달력만들기>

//달력만들기 소스

package com.day4;

import java.util.Scanner;

public class Test5 {

	public static void main(String[] args) {

		
		//만년 달력
				
		Scanner sc = new Scanner(System.in);	
		
		int months[] = {31,28,31,30,31,30,31,31,30,31,30,31}; // 초기화 및 방 형성
		int y,m,nalsu,i,week;
		
		do{
			System.out.print("년도? ");//2018
			y=sc.nextInt();
		}while(y<1);
		
		do{
			System.out.print("월은? ");//12
			m=sc.nextInt();
		}while(m<1||m>12);
		
		//윤년에 따른 2월의 날수 계산
		if(y%4==0 && y%100!=0 ||y%400==0){
			months[1] = 29;
		}
		
		//1년 1월 1일부터 y-1년 12월 31일까지의 날수
		
		nalsu = (y-1) * 365 + (y-1)/4-(y-1)/100+(y-1)/400;

/*		
		int yy = (y-1)/4-(y-1)/100+(y-1)/400;
		System.out.println(nalsu);
		System.out.println(yy);
*/
		
		//월   : 1  2  3  4  5  6  7  8  9  10 11 12
		//배열 : 31,28,31,30,31,30,31,31,30,31,30,31
		//idx  : 0  1  2  3  4  5  6  7  8  9  10 11
		for(i=0;i<(m-1);i++){
			
			//nalsu = nalsu + months[i];
			nalsu += months[i];
			
		}
						
		//System.out.println(nalsu);
	
		//1년 1월 1일부터 y년 m월 1일까지의 날수
		nalsu += 1; //nalsu = nalsu + 1;
		
		//y년 m월 1일의 주의 수 계산
		
		week = nalsu%7;
		
		//System.out.println(week);
		
		System.out.println("            " + y +"-"+ m);
		
		System.out.println("\n  일  월  화  수  목  금  토");
		System.out.println("------------------------------");
		
		//공백지정
		for(i=0;i<week;i++){
			System.out.print("    ");//공백4칸
		}
		
		//월   : 1  2  3  4  5  6  7  8  9  10 11 12
		//배열 : 31,28,31,30,31,30,31,31,30,31,30,31
		//idx  : 0  1  2  3  4  5  6  7  8  9  10 11
		//해당 월의 날짜 출력
		for(i=1;i<=months[m-1];i++){
			
			System.out.printf("%4d",i);//%4d : 자릿수 4자리(4byte) 만들어줌.
			
			week++;
			if(week%7==0)
				System.out.println();
						
		}
		if(week%7!=0)
			System.out.println();
		
		System.out.println("------------------------------");

		sc.close();
		
	}
	
}

==Console==

<사용자에게 입력받아 요일까지 출력하기>

//소스코드

package com.day4;

import java.util.Scanner;

public class Test6 {

	public static void main(String[] args) {

		//년,월,일을 입력받아 요일을 출력하시오.
		//2018년 12월 3일 월요일
		
		Scanner sc = new Scanner(System.in);
		
		int months[] = {31,28,31,30,31,30,31,31,30,31,30,31};
		int y,m,d,week,nalsu,i;
		char ch[] = {'일','월','화','수','목','금','토'};
				
		do{
			System.out.println("년도를 입력하시오. ");
			y = sc.nextInt();
		}while(y<1);
		
		do{
			System.out.println("월을 입력하시오. ");
			m = sc.nextInt();
		}while(m<1 || m>12);
		
		do{
			System.out.println("일을 입력하시오. ");
			d = sc.nextInt();
		}while(d<1 || d>months[m-1]);
		
		if(y%4 ==0 && y%100!=0 || y%400==0){
			months[1] = 29;
		}
		
		nalsu =  (y-1) * 365 + (y-1)/4-(y-1)/100+(y-1)/400;
		
		for(i=0;i<(m-1);i++){
			
			nalsu += months[i];
			
		}
		
		nalsu += d;
			
		week = nalsu%7;
		
		System.out.println(y + "년 " + m + "월 " + d + "일 " + ch[week]+"요일");
		
		sc.close();
	}

}

 

==Console==

+ Recent posts