[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 = "์—ฌ์ž";
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ