[Day12] Java 12 [8/24]

 

1.  ๋ฐฐ์—ด, ๋ฐ˜๋ณต๋ฌธ, ์ž…๋ ฅ

package days12;

import java.util.Random;
import java.util.Scanner;

/**
 * @author k≡n¡k
 * @date 2022. 8. 24. - ์˜ค์ „ 10:34:46
 * @subject  ๋ฐฐ์—ด~ 
 * @content 
 *
 */
public class Ex02 {

	public static void main(String[] args) {
		// ํ•œ ํ•™์ƒ์˜ ์ด๋ฆ„,๊ตญ์–ด,์˜์–ด,์ˆ˜ํ•™,์ด์ ,ํ‰๊ท ,๋“ฑ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ์ฝ”๋”ฉ~ 
		//String name;
		//int kor, eng, mat, tot, rank;
		//double avg;
		
		// ํ•œ ๋ฐ˜์— 30๋ช… ํ•™์ƒ
		// ์ด๋ฆ„,๊ตญ์–ด,์˜์–ด,์ˆ˜ํ•™,์ด์ ,ํ‰๊ท ,๋“ฑ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ์ฝ”๋”ฉ
		// String name1, name2,name3,name4~ name30;
		// int kor1, kor2~ kor30;
		// 30๊ฐœ
		// ๋ฐฐ์—ด ์‚ฌ์šฉ. 
		
		final int STUDENT_COUNT = 30;
		
		String [] names = new String[STUDENT_COUNT];
		int [] kors = new int[STUDENT_COUNT];
		int [] engs = new int[STUDENT_COUNT];
		int [] mats = new int[STUDENT_COUNT];
		int [] tots = new int[STUDENT_COUNT];
		int [] ranks = new int[STUDENT_COUNT];
		double [] avgs = new double[STUDENT_COUNT];
		
		int count = 0; // == ์ž…๋ ฅ๋ฐ›์€ ํ•™์ƒ ์ˆ˜ (์˜๋ฏธ)     index ๋ณ€์ˆ˜ ๋™์ผํ•˜๋‹ค. 
		char con = 'y'; 
		Scanner scanner = new Scanner(System.in); 
		 
		do {
			// 11:05 ์ˆ˜์—… ์‹œ์ž‘~ 
			System.out.printf("> [%d]๋ฒˆ ํ•™์ƒ ์ด๋ฆ„,๊ตญ,์˜,์ˆ˜ ์ž…๋ ฅ ? ", count+1);
			
			String name = getRandomName(); // scanner.next();
			int kor = getRandomScore(); //  scanner.nextInt();
			int eng = getRandomScore(); //  scanner.nextInt();
			int mat = getRandomScore(); //  scanner.nextInt();
			
			int tot = kor + eng + mat;
			double avg = (double)tot / 3;
			int rank = 1;
			
			// 
			names[count] = name;
			kors[count]=kor;
			engs[count]=eng;
			mats[count]=mat;
			tots[count]=tot;
			avgs[count]=avg;
			ranks[count]=rank;
			
			count++;  // 2
			System.out.print("> ํ•™์ƒ ์ž…๋ ฅ ๊ณ„์† ํ• ๋ž˜ ? ");
			con =  scanner.next().charAt(0);
		} while (   Character.toUpperCase( con )  == 'Y'  );
	 
	 
		
		System.out.printf("์ด ํ•™์ƒ์ˆ˜ : %d\n" , count );
		
		// 12:05๋ถ„ ์ˆ˜์—…์‹œ์ž‘~
		// ์ „์ฒด ํ•™์ƒ์˜ ๋“ฑ์ˆ˜ ์ฒ˜๋ฆฌ. - 1๋ฒˆ
		// ๋“ฑ์ˆ˜์ฒ˜๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ฒ˜๋ฆฌํ•˜์„ธ์š”. 
		// 1) ๊ธฐ๋Šฅ  2) ๋งค๊ฐœ๋ณ€์ˆ˜ ?  3) ๋ฆฌํ„ด๊ฐ’(๋ฆฌํ„ด์ž๋ฃŒํ˜•)
		ranks = getRank(tots, ranks, count);
		
		// ํ•™์ƒ ์ •๋ณด ์ถœ๋ ฅ
		for (int i = 0; i < count ; i++) {
			System.out.printf("%s\t%d\t%d\t%d\t%d\t%.2f\t%d\n", 
					names[i], kors[i], engs[i],mats[i], tots[i], avgs[i], ranks[i]
					);
		}

		
		// toUpperCase() ํ•จ์ˆ˜๋Š” ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•ด์„œ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ 
		// toLowerCase()          ์†Œ๋ฌธ์ž
		//System.out.println(  Character.toUpperCase( 'y')  );  //   'Y'
		//System.out.println(  Character.toUpperCase( 'Y')  ); //    'Y'
	} // main
	
	public static int []  getRank( int [] tots, int [] ranks , int count) {
		for (int i = 0; i < count; i++) {        // i  ๋“ฑ์ˆ˜๋งค๊ธธ ํ•™์ƒ 
			ranks[i] = 1; // ์ค‘์š”
			for (int j = 0; j < count; j++) {     
				if( tots[i] < tots[j] ) ranks[i]++;	
			} 
		}
		return ranks;
	}
	
	// 0~100 ์ ์ˆ˜ 
	public static int getRandomScore() {
		Random rnd  = new Random();
		return rnd.nextInt( 101 );  // 0<=   int  <101
	}
	
	// ์ด๋ฆ„ 
	public static String getRandomName() {
		String name = null;
		Random rnd  = new Random();
		// 'A'~'Z'  ๋ฌด์ž‘์œ„ 5๊ฐœ ๋ฌธ์ž ๋ฐœ์ƒ -> String name ๋ณ€ํ™˜
		// 65~90
		char [] names = new char[5];		
		for (int i = 0; i < names.length; i++) {
			names[i] =  (char) (rnd.nextInt(26)+65);
		}
		
		// char [] -> String ๋ณ€ํ™˜
		// name = String.valueOf(names);
		name = new String( names ); 
		
		return name;
	}

} // class

2.  ์š”์†Œ ์ถ”๊ฐ€ / ์‚ญ์ œ / ์‚ฝ์ž…

	public static void main(String[] args) {
		// ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”.
		int [] m = { 1,2,3,4,5};
		
		System.out.println( Arrays.toString( m ) );
		
		// 1. ์š”์†Œ ์ถ”๊ฐ€
		int index = 5;
		if( index == m.length ) m = Ex01.increaseArraySize(m, 3);
		m[index] = 6;
		System.out.println( Arrays.toString( m ) );
		index++;
		
		// 2. ์š”์†Œ ์‚ญ์ œ     - index = 2
		// [1, 2, (3), 4, 5, 6, 0, 0]
		// [1, 2, 4, 5, 6, 0, 0,0]
		for (int i = 3; i < m.length; i++) {
			m[i-1]= m[i];
		}
		m[m.length-1] = 0;
		System.out.println( Arrays.toString( m ) );
		index--;
		
		// 3. ์š”์†Œ ์‚ฝ์ž…
		// [1, 2, 4, 5, 6, 0, 0, 7]
		// 1)  ๋ฐฐ์—ด ํฌ๊ธฐ ์ฆ๊ฐ€ ์ฒดํฌ
		// 2) ์‚ฝ์ž…ํ•  ์œ„์น˜ - 3   ,  100 ์ •์ˆ˜ ์‚ฝ์ž…
		// [1, 2, 4 , (100), 5, 6, 0, 0]
		
		// if(  m[m.length-1]  != 0  )  m = Ex01.increaseArraySize(m, 3);
		// ๊ฐ€์ • 0
		// [1, 2, 4, 5, 6, 0, 0, 0]
		//	[1, 2, 4, 100, 5, 5, 5, 5]

		if( index == m.length ) m = Ex01.increaseArraySize(m, 3);

		for (int i = index -1 ; i >= 3 ; i--) {
			m[i+1] = m[i]; 
		}
		
		m[3]=100;
		System.out.println( Arrays.toString( m ) );
		index++;
		
		
		m[0] = 1000;
		
	} // main

3.  ์ตœ๋Œ€๊ฐ’, ์ตœ์†Œ๊ฐ’ ์ฐพ์•„ ์ถœ๋ ฅ

public static void main(String[] args) {
		// ๋ฒ”์œ„ 0~100 ์ •ํ•ด์ ธ ์žˆ์œผ๋ฉด X 
		int [] score = { 79, 88, 91, 33, 100, 55, 95 };		
		// ์ ์ˆ˜    : ์ตœ๋Œ€๊ฐ’, ์ตœ์†Œ๊ฐ’   ์ฐพ์•„์„œ ์ถœ๋ ฅ.
		// int max = -1, min = 101; 
	    int max , min;
	    max = min = score[0];  // 79
	    
	    for (int i = 1; i < score.length; i++) {
			if( max < score[i])   max = score[i];
			else if( min > score[i] ) min = score[i];
		}
	    
	    System.out.printf("์ตœ๋Œ€๊ฐ’:%d, ์ตœ์†Œ๊ฐ’:%d\n", max, min);		
		

	} // main

4.  ์นด๋“œ์„ž๊ธฐ

public static void main(String[] args) {
		// ์นด๋“œ ์„ž๊ธฐ,  ํ™”ํˆฌ ์„ž๊ธฐ
		int [] m = new int[10];
		for (int i = 0; i < m.length; i++) {
			m[i] = i+1;
		}

		System.out.println( Arrays.toString( m ) );

		shuffle(m); // ์ •๋‹ต
		
		System.out.println( Arrays.toString( m ) );


	} // main

	// ๋žœ๋ค ์„ž๊ธฐ ํ•จ์ˆ˜
	public static void shuffle(int [] m) {
		// ์ˆซ์ž ์„ž๊ธฐ
		Random rnd = new Random();
		int a, b, temp;
		for (int i = 0; i < 100; i++) {
			a =  rnd.nextInt(10);  // 4
			b =  rnd.nextInt(10);  // 8			
			temp = m[a];
			m[a] = m[b];
			m[b] = temp;
		}		
		 
	}

5.  ๊ฐ€๋ณ€ ๋ฐฐ์—ด

- ๋‹ค์ฐจ์› ๋ฐฐ์—ด์—์„œ ์ „์ฒด ๋ฐฐ์—ด ์ฐจ์ˆ˜ ์ค‘์— ๋งˆ์ง€๋ง‰ ์ฐจ์ˆ˜์˜ ๊ธธ์ด๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์€ ๋ฐฐ์—ด

- ์ถ”ํ›„์— ๊ฐœ๋ฐœ์ž๊ฐ€ ๋งˆ์ง€๋ง‰ ์ฐจ์ˆ˜๋ฅผ ์ง€์ •ํ•  ์˜๋„

int [][] m = new int[3][]; // ๊ฐ€๋ณ€๋ฐฐ์—ด               ์—ด == ๋งˆ์ง€๋ง‰์ฐจ์ˆ˜
		m[0] = new int[10];
		m[1] = new int[5];
		m[2] = new int[7];

 

6. ๋žœ๋ค ์ธ๋ฑ์Šค ํ†ตํ•ด์„œ ๋ฌธ์ œ ๋žœ๋ค์œผ๋กœ ๋‚˜์˜ค๊ฒŒ ํ•˜๊ธฐ

public static void main(String[] args) {
		// ์ •๋ณด์ฒ˜๋ฆฌ๊ธฐ์‚ฌ
		// [๋ฌธ์ œ] -> ์ œ์–ด๋ฌธ(ํ•˜2์ค‘2์ƒ1),์—ฐ์‚ฐ์ž,๋ฐฐ์—ด ->
		// ์˜์–ด๋‹จ์–ด -> ์ถœ๋ ฅ : ๋œป  ์ฒดํฌํ•˜๋Š” ์ฝ”๋”ฉ.   - ๋ฐฐ์—ด
		String [][] words = {
				{"chair","์˜์ž"},  // ๊ฐ ํ–‰๋งˆ๋‹ค   0์—ด ์˜์–ด๋‹จ์–ด, 1์—ด ๋œป
				{"computer", "์ปดํ“จํ„ฐ"},
				{"integer", "์ •์ˆ˜"} 
		};		
		// ๋žœ๋คํ•˜๊ฒŒ 1๋ฌธ์ œ ์ถœ์ œ 
		// ํ–‰ ๊ฐฏ์ˆ˜ :3  ==   3๋ฌธ์ œ
		// ํ–‰ ๊ฐฏ์ˆ˜ ๋žœ๋คํ•˜๊ฒŒ ๋ฐœ์ƒ     0  , 1 , 2
		Random rnd = new Random();
		Scanner scanner = new Scanner(System.in);
		
		// ๋‹จ์–ด ๋ฌธ์ œ 10๊ฐœ -> ์ •๋‹ต 6๊ฐœ   ์ƒ๊ฐ(๊ตฌํ˜„)
		char con = 'y';
		do {
			
			 int rowIndex = rnd.nextInt(3);
			 String ๋ฌธ์ œ๋‹จ์–ด = words[rowIndex][0];
			 System.out.printf("\"%s\" ๋‹จ์–ด์˜ ๋œป์„ ์ ์œผ์„ธ์š” ? ", ๋ฌธ์ œ๋‹จ์–ด );
			 String word= scanner.next();
			 
			 if( words[rowIndex][1].equals(word) ) System.out.println("์ •๋‹ต");
			 else System.out.println("์˜ค๋‹ต");
			 
			 System.out.print("> ๋ฌธ์ œ ๊ณ„์†ํ• ๋ž˜? ");
			 con = scanner.next().charAt(0);
		} while ( Character.toUpperCase(con) =='Y');
		
		System.out.println("end");

 

  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ