[Day11] Java 11 [8/23]
1. ์ฐ์ ์ ๋ ฅ๋ฐ์์ ๋ฌ๋ ฅ์ถ๋ ฅ
package days11;
import java.util.Scanner;
import days08.Ex04_02;
/**
* @author k≡n¡k
* @date 2022. 8. 23. - ์ค์ 10:08:53
* @subject ๋ง๋
๋ฌ๋ ฅ[ ์ํ ]
* @content ๋
๋,์ ์
๋ ฅ
*
*/
public class Ex02_02 {
public static void main(String[] args) {
int year, month;
int [] ym = getYearMonth();
year = ym[0];
month = ym[1];
printCalendar(year, month);
} // main
private static void printCalendar(int year, int month) {
int dayOfWeek = getDayOfWeek(year, month); // 0~6 ์ ์
int lastDay = getLastDay(year, month);
// ใท. ๋ฌ๋ ฅ ์ถ๋ ฅ
System.out.printf("\t\t%d๋
%d์\n", year, month);
String week = "์ผ์ํ์๋ชฉ๊ธํ ";
for (int i = 0; i < week.length() ; i++) {
System.out.printf("\t%c", week.charAt(i));
}
System.out.println();
// ๋ฌ๋ ฅ 1~ ๋ง์ง๋ง๋ ์ง ์ถ๋ ฅ.
// ๊ณต๋ฐฑ for
for (int i = 0; i < dayOfWeek; i++) {
System.out.print("\t");
}
// ASCII ์ถ๋ ฅ
// ํ ๋ผ์ธ์ 10๊ฐ์ฉ
// 10๋ผ์ธ ์ถ๋ ฅ ์ผ์ ๋ฉ์ถค + ์ํฐ
for (int i = 1; i <=lastDay ; i++) {
System.out.printf("\t%d", i);
// ๊ฐํ
if( ( i+ dayOfWeek ) % 7 == 0) System.out.println();
}
System.out.println(); // ๊ฐํ
}
public static int getDayOfWeek(int year, int month ) {
int totalDays = getTotalDays(year, month, 1 );
int dayOfWeek = totalDays % 7 ;
return dayOfWeek;
}
private static int getTotalDays(int year, int month, int day) {
int totalDays = 0;
totalDays = 365 * ( year-1 ) + ( year-1)/4 - ( year-1)/100 + ( year-1)/400;
for (int i = 1; i < month; i++) totalDays += getLastDay(year, i);
totalDays += day;
return totalDays;
}
public static int getLastDay(int year, int month ) {
int [] months = {31,28,31,30,31,30,31,31,30,31,30,31};
return month==2 && Ex04_02.isLeapYear(year) ? 29 : months[ month-1 ];
}
public static int [] getYearMonth() {
int [] ym = new int[2];
Scanner scanner = new Scanner(System.in);
System.out.print("๋
๋ ์ ์
๋ ฅ ? ");
ym[0] = scanner.nextInt();
ym[1] = scanner.nextInt();
return ym;
}
} // class
2. ๋ฐฐ์ด
1) ์ธ๋ฑ์ค ์ฐ์ฐ์ ์ฌ์ฉ
2) ์ฐธ์กฐํ ์๋ฃํ
3) ์ค์ ๊ธฐ์ต๊ณต๊ฐ์ new์ฐ์ฐ์์ ์ํด ๋์ (heap)์์ญ์ ํ ๋น๋๊ณ ์ฃผ์๋ฅผ ์คํ(stack)์์ญ์์ ์ฐธ์กฐ
4) ์ ์ : ๋์ผํ ์๋ฃํ์ ๋ฉ๋ชจ๋ฆฌ์์ ์ฐ์์ ์ผ๋ก ๋์ด๊ธฐ ํ ๊ฒ
5) ์ ์ธ : ์๋ฃํ [] ๋ฐฐ์ด๋ช = new ์๋ฃํ [๋ฐฐ์ดํฌ๊ธฐ]
int [] kors = new int[5000];
6) ๋ฐฐ์ดํฌ๊ธฐ : kors.length
7) ๋ฐฐ์ด์ ์ด๊ธฐํํ์ง ์์๋ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ด๊ธฐํ๋์ด์๊ธฐ ๋๋ฌธ์ ๋ฐ๋ก ์ฌ์ฉ๊ฐ๋ฅ
8) ์ด๊ธฐํ ๋ฐฉ๋ฒ
int [] m = new int[3];
m[0] = 10;
m[1] = 20;
m[2] = 30;
int [] m = new int[]{1,2,3};
int [] m = {1,2,3};
3. N์ฐจ์ ๋ฐฐ์ด
1) 2์ฐจ์ ๋ฐฐ์ด
int [][] m = new int[2][4]
์ด๊ธฐํ
int [][] m = new int[][] { {1,2,3,4}, {5,6,7,8} };
int [][] m = {
{1,2,3,4},
{5,6,7,8}
};
2) 3์ฐจ์ ๋ฐฐ์ด
int [][][] m = new int[2][3][4];
์ด๊ธฐํ
int [][][] m = {
{
{1,2,3,4},
{5,6,7,8}
},
{
{ 11,12,13,14},
{ 15,16,17,18}
}
};
public static void main(String[] args) {
int [] m = new int[3];
int index = 0;
char con = 'y'; // 'n'
Scanner scanner = new Scanner(System.in);
do {
// ๋ฐฐ์ด ํฌ๊ธฐ ๋ค ์ฐผ๋์ง
if( m.length == index ) {
int [] temp = new int[m.length + 3];
// m -> temp ๋ณต์ฌ
System.arraycopy(m, 0, temp, 0, m.length);
m = temp;
}
System.out.printf("> m[%d]์ ์ ์ ์
๋ ฅ ? ", index);
m[index++] = scanner.nextInt();
System.out.print("> ์
๋ ฅ ๊ณ์ํฉ๋๊น? ");
con = scanner.next().charAt(0); // "y" -> 'y'
} while ( ( con == 'y' || con =='Y' ) );
System.out.println( Arrays.toString( m ) );
} // main
๋ฐฐ์ด ๋ณต์ฌ
System.arraycopy(m, 0, temp, 0, m.length);
'๐จโ๐ป Web Development > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Day13] Java 13 - ์ ๋ ฌ, ์ธ๋ฑ์ค(๋ฉ์๋ํ์ฉX) (0) | 2022.09.06 |
---|---|
[Day12] Java 12 - ๋ฐฐ์ด์ ํ์ฉ (0) | 2022.09.01 |
[Day10] Java 10 - ์ง์ ๋ณํ (0) | 2022.09.01 |
[Day9] Java 9 - ๋ก๋, ํฉํ ๋ฆฌ์ผ, ์นด๋๋ฒํธ (0) | 2022.09.01 |
[Day8] Java 8 - ์ ๊ทํํ์(regex), ๋ฉ์๋, ์ค๋ฒ๋ก๋ฉ, swap (0) | 2022.09.01 |
์ต๊ทผ๋๊ธ