[Day9] Java 9 [8/19]
1. ๋ก๋
์ค๋ณตํ์ธ
public static void fillLotto(int[] lotto) {
// for ๋ฌธ ์ฌ์ฉ 6 ๋ฒ ๋ฐ๋ณต ( int i = 0; i < lotto.length ; ) X
// while ๋ฌธ ์กฐ๊ฑด๋ฐ๋ณต๋ฌธ - ๋ฐฐ์ด ๋ก๋๋ฒํธ ๋ชจ๋ ์ฑ์๋ฃ์ ๋ ๊น์ง.. ์กฐ๊ฑด
Random rnd = new Random();
int index = 0;
int temp ;
lotto[0] = rnd.nextInt(45) + 1;
System.out.println( lotto[0 ] );
index = 1;
while( index <= 5) {
temp = rnd.nextInt(45) + 1;
System.out.println( temp );
// 10:09 ์์
์์
// ์ค๋ณต์ด๋๋ฉด true
// ์ค๋ณต์ด ๋์ง์์ผ๋ฉด false
if( !isDuplicateLotto( lotto , temp, index ) ) {
lotto[index] = temp;
index++;
} // if
} // while
}
private static boolean isDuplicateLotto(int[] lotto, int temp, int index) {
for (int i = 0; i < index ; i++) {
if( lotto[i] == temp ) return true;
}
return false;
}
public static void dispLotto(int[] lotto) {
for (int i = 0; i < lotto.length; i++) {
System.out.printf("[%d]", lotto[i]);
} //
System.out.println();
} // dispLotto
2. ํฉํ ๋ฆฌ์ผ
ํฉํ ๋ฆฌ์ผ (recursive, iterative)
public static void main(String[] args) {
int n = -5;
// int result = factorial( n ) ;
// recursiveSum()
int result = recursiveFactorial(n);
System.out.printf( "%d!=%d\n ", n, result );
} // main
// -5!
// ์ฝ์ 0! = 1
private static int recursiveFactorial(int n) {
if( n == 0) return 1;
//else if( n == 0 ) return 1;
else if( n < 0 ) return -1; // ์๋ฌ ... ๊ฐ์
else return n * recursiveFactorial( n - 1);
// 5*4*3*2*1*1
}
private static int factorial(int n) {
// 1*2*3*4*5= 5!
int result = 1;
for (int i = 1; i <= n; i++) {
//System.out.printf("%d*", i);
result *= i;
}
//System.out.println();
return result;
}
3. ์นด๋ ๋ฒํธ ๋๋ค ์จ๊ธฐ๊ธฐ
String card = "7566-8988-9234-5677";
// ๊ฒฐ์ฌํ ๋ ๋ง๋ค ์์์ฆ ์นด๋๋ฒํธ ์ธ์ - ๋๋คํ๊ฒ **** ๋ก ๋ณํ.
// "7566-****-9234-5677"
// "7566-8988-9234-****"
// "7566-8988-****-5677"
// "****-8988-9234-5677"
/*
* 1. card ๋ฌธ์์ด์ '-' ๊ตฌ๋ถ์๋ก ๋ฌธ์์ด ์๋ผ๋ด๊ธฐ : String [] split()
*
7566 [0]
8988 [1]
**** [2]
5677 [3]
2. ๋๋คํ๊ฒ ์ ์ 0~3 -> 2
**** [2] 2 ์ธ๋ฑ์ค์ ๋ฌธ์์ด -> "****" ๋ณ๊ฒฝ
*
3. [0]-[1]-[2]-[3]
7566-8988-****-5677
* */
String regex = "-";
String [] cards = card.split(regex);
Random rnd = new Random();
// bound 4 0<= int < 4
int index = rnd.nextInt(4);
cards[index] = "****";
//System.out.printf( "%s-%s-%s-%s\n", cards[0], cards[1], cards[2], cards[3] );
// %s-%s-%s-%s ํ์์ ๋ฌธ์์ด๋ก ๋ณํ
// ***** String.format() *****
//String changedCard = String.format("%s-%s-%s-%s" , cards[0], cards[1], cards[2], cards[3] );
// ***** String.join() *****
String changedCard = String.join("-", cards);
System.out.println( changedCard );
4. String -> char[], char[] -> String
char [] nameCharArray = new char[ len ]; // 5 [][][][][] char []
nameCharArray[0]= name.charAt(0);
nameCharArray[1]= name.charAt(1);
nameCharArray[2]= name.charAt(2);
nameCharArray[3]= name.charAt(3);
nameCharArray[4]= name.charAt(4);
// char [] ํ์ธ
// Arrays.toString() ๋ฐฐ์ด ์์ ์์์ ๊ฐ๋ค์ ๋ฌธ์์ด๋ก ๋ณํํด์ ๋ฐํํ๋ ๋ฉ์๋ "[k, e, n, i, k]"
// ๋ฐฐ์ด ๊ฐ์ ํ์ธ ํ ๋ ์์ฃผ ์ฌ์ฉํ ์์
System.out.println( Arrays.toString( nameCharArray) ) ;
String name2 = String.valueOf(nameCharArray);
5. ์ฃผ๋ฏผ๋ฒํธ ํ์ธ
String rrn = "980221-1700001";
// 1. charAt(index)
// char gender = rrn.charAt( 7 ) ; // '1'
// 2. substring()
String ใ
= rrn.substring(7, 8); // "1"
String gender = "";
// System.out.println( gender ); // "1"
if( Integer.parseInt(ใ
) % 2 != 0 ) gender = "๋จ์";
else gender = "์ฌ์";
'๐จโ๐ป Web Development > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Day11] Java 11 - ๋ฌ๋ ฅ์ถ๋ ฅ, ๋ฐฐ์ด, N์ฐจ์ ๋ฐฐ์ด (0) | 2022.09.01 |
---|---|
[Day10] Java 10 - ์ง์ ๋ณํ (0) | 2022.09.01 |
[Day8] Java 8 - ์ ๊ทํํ์(regex), ๋ฉ์๋, ์ค๋ฒ๋ก๋ฉ, swap (0) | 2022.09.01 |
[Day7] Java 7 - ๋ฌ๋ ฅ, ๊ตฌ๊ตฌ๋จ (0) | 2022.08.29 |
[Day6] Java 6 - String char๊ฐ ํ์ธ, ์์คํค/์ ๋์ฝ๋ (0) | 2022.08.29 |
์ต๊ทผ๋๊ธ