๐Ÿฐ[JAVA]2 D้…ๅˆ—๐Ÿฐ


#2 D้…ๅˆ—ๅ‰ใซforใ‚ฒใƒผใƒˆใ‚’้‡ใญใฆๅพฉ็ฟ’

  • ใ‚ฒใƒผใƒˆใ‚ชใƒผใƒใƒผใƒฉใƒƒใƒ—(forใ‚ฒใƒผใƒˆๅ†…ใซforใ‚ฒใƒผใƒˆใ‚ชใƒผใƒใƒผใƒฉใƒƒใƒ—)
  • ใฎๅค–ใฎforๆ–‡ใ‚’1ๅ›ž็นฐใ‚Š่ฟ”ใ™ใ”ใจใซใ€้‡ใชใ‚‹forๆ–‡ใฏi~nใซ็นฐใ‚Š่ฟ”ใ•ใ‚Œใ‚‹.
  • ใฎ2ๆฌกๅ…ƒ้…ๅˆ—ใงใฏใ€ใƒ‰ใ‚ขใฎใ‚ชใƒผใƒใƒผใƒฉใƒƒใƒ—ใฏไธๅฏๆฌ ใชไธ€ๆญฉใงใ‚ใ‚‹.

  • 1.2 Dใ‚ขใƒฌใ‚ค

  • 2 2ๆฌกๅ…ƒ้…ๅˆ—ใฏใ€ใ‚ชใƒผใƒใƒผใƒฉใƒƒใƒ—ๆ–‡ใ‚’ไฝฟ็”จใ—ใฆ่กŒ/ๅˆ—
  • ใจใ—ใฆ่กจใ™.
  • ่กŒใฏ็ธฆ็ทšใ€ๅˆ—ใฏๆจช็ทšใงใ™.
  • ใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚น็•ชๅทใฏใ‚ผใƒญใ‹ใ‚‰ๅง‹ใพใ‚Šใพใ™.
  • ใƒกใ‚ฝใƒƒใƒ‰ใฎๅฎฃ่จ€
    (1)ใƒ‡ใƒผใ‚ฟๅž‹ๅค‰ๆ•ฐๅ[]=newใƒ‡ใƒผใ‚ฟๅž‹[];
    (2)ใƒ‡ใƒผใ‚ฟๅž‹[][ๅค‰ๆ•ฐๅ=newใƒ‡ใƒผใ‚ฟๅž‹[];
    ex) int data[][] = new int[3][5];//3่กŒ5ๅˆ—
    +ๅˆๆœŸๅŒ–
    (1)ใƒ‡ใƒผใ‚ฟๅž‹[][ๅค‰ๆ•ฐๅ={0,0,0,0}{0,0}
    =>2่กŒ4ๅˆ—[2][4]

    1-1. 2 Dใ‚ขใƒฌใ‚คใฎไพ‹

    //๋‹ค์Œ์˜ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ ํ›„ ์ถœ๋ ฅํ•˜์‹œ์˜ค.
    //10 10 10
    //20 20 20
    //30 30 30
    //40 40 40
    
    public class ArrayExam1 {
    
    	public static void main(String[] args) {
    		int num[][] = new int[4][3]; //4ํ–‰ 3์—ด
    		for(int i = 0; i < num.length;i++) {
    			for(int j = 0; j < num[i].length; j++) {
    				System.out.print(10+(i*10)+"\t");
    			}
    			System.out.println();
    		}
    	}
    
    }
    
    //ํ™œ์šฉ์˜ˆ์ œ
    /*
    * 1.์ž…๋ ฅ intputScore
    * 2.์ด์  getSumScore
    * 3.์ˆœ์œ„ getRank
    * 4.์ถœ๋ ฅ printResult
    */
    /*
    ===========================================
    * [0] => [0|0]  [0|1]  [0|2]   [0|3]  [0|4]
    * [1] => [1|0]  [1|1]  [1|2]   [1|3]  [1|4]
    * [2] => [2|0]  [2|1]  [2|2]   [2|3]  [2|4]
    * [3] => [3|0]  [3|1]  [3|2]   [3|3]  [3|4]
    * [4] => [4|0]  [4|1]  [4|2]   [4|3]  [4|4]	 
    ============================================
       ์กฐ๋ฒˆํ˜ธ     	์ดํ•ด๋„	์ฐฝ์˜๋„	์™„์„ฑ๋„ 	์ด์  	 ๋“ฑ์ˆ˜
    ===========================================
    *1 [0] => [0|0]  [0|1]  [0|2]   [0|3]  [0|4]
    *2 [1] => [1|0]  [1|1]  [1|2]   [1|3]  [1|4]
    *3 [2] => [2|0]  [2|1]  [2|2]   [2|3]  [2|4]
    *4 [3] => [3|0]  [3|1]  [3|2]   [3|3]  [3|4]
    *5 [4] => [4|0]  [4|1]  [4|2]   [4|3]  [4|4]	 
    ============================================
    */
    import java.util.Scanner;
    public class Array2_score1 {
       public static void main(String[] args) {
        
          Scanner scan = new Scanner(System.in);
          int score[][]=new int[5][5]; // ์„ ์–ธ๋ถ€๋ถ„  5ํ–‰ 5์—ด 
          intputScore(score); //์ž…๋ ฅ
          getSumScore(score); //์ด์ 
          getRank(score);  //์ˆœ์œ„
          printResult(score); //์ถœ๋ ฅ
       }
       
        public static int[][] intputScore(int[][] score) {  //์ž…๋ ฅ
          Scanner scan=new Scanner(System.in);
          
          for(int i=0; i<score.length; i++){   //์ž…๋ ฅ์„ 5๋ฒˆ ๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก ๋ฐฐ์—ด ๊ธธ์ด 0,1,2,3,4 ๋ฐ˜๋ณต
              System.out.println((i+1)+"ํŒ€์˜ ํ•ญ๋ชฉ๋ณ„ ์ ์ˆ˜ ์ž…๋ ฅ,\n๋‹จ(0๋ถ€ํ„ฐ 20๊นŒ์ง€ ์ž…๋ ฅ ๊ฐ€๋Šฅ)");
              //i+1์€ 1์กฐ 1+1์กฐ 2+1์กฐ๊ฐ€ ๋˜๋„๋ก => 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ +1์„ ํ•ด์ค€๋‹ค. 
              for(int j=0; j<score.length-2; j++){  //๋ฐฐ์—ด 5๊ฐœ ์ค‘ 3๊ฐœ๋งŒ ๋ฐ›์•„์•ผ ํ•˜๋ฏ€๋กœ (์™„์„ฑ๋„ ์ฐฝ์˜์„ฑ ์ดํ•ด๋„๋งŒ ๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก*์ด์  ์„์ฐจ ์ œ์™ธ)
            	  switch(j+1) {  //if๋ฌธ์ด๋ž‘ ๋น„์Šทํ•œ๋ฐ ๊ด„ํ˜ธ ์•ˆ์ด ๊ฐ’์ด ์กฐ๊ฑด์ด๋‹ค. => i๋ฐฐ์—ด 1๋ฒˆ ์‹คํ–‰ ํ•  ๋•Œ j๋ฐฐ์—ด 3๋ฒˆ ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก, j๋ฐฐ์—ด ์‹œ ์ฆ๊ฐ ๋˜๋ฏ€๋กœ 1ใ…ก2ใ…ก3์ด ๋‹ค ์ž…๋ ฅํ•˜๊ฒŒ ๋จ 
            	  case 1: System.out.print("์™„์„ฑ๋„ ์ž…๋ ฅ:"); break;
            	  case 2: System.out.print("์ฐฝ์˜์„ฑ ์ž…๋ ฅ:"); break;
            	  case 3: System.out.print("์ดํ•ด๋„ ์ž…๋ ฅ:"); break;
            	  }
                 score[i][j]=scan.nextInt();  
                 
                 while(score[i][j]>20||score[i][j]<0) {   //20์ดˆ๊ณผ ์ด๊ฑฐ๋‚˜ 0๋ฏธ๋งŒ์ผ ์‹œ ๋‹ค์‹œ ์ž…๋ ฅ
                    System.out.println("๋‹ค์‹œ ์ž…๋ ฅ:");
                      score[i][j]=scan.nextInt();
                 }
               }
          }
          return score; //์ž…๋ ฅ๋ฐ›์€ ๊ฐ ์กฐ์˜ ์™„์„ฑ๋„ ์ฐฝ์˜์„ฑ ์ดํ•ด๋„๋ฅผ main๋ฉ”์„œ๋“œ์— ๋ฆฌํ„ด ์‹œ์ผœ์ค€๋‹ค. 
        }
        
       public static int[][] getSumScore(int[][] score) {   //์ด์  
          //์ด์ 
    	   int[] sum=new int[5];   //5ํŒ€์˜ ์ ์ˆ˜๋ฅผ ๋„ฃ์„ ๋ฐฐ์—ด 
    	   int temp;  //์ด์ ์„ ๋„ฃ์„ ๋นˆ๊ณต๊ฐ„ 
           for(int i=0; i<score.length; i++){  //5ํŒ€์ด์—ฌ์„œ 5๋ฒˆ ๋ฐ˜๋ณต 
                 for(int j=0; j<score.length-1; j++){ //์™„์„ฑ.์ฐฝ์˜.์ดํ•ด๋„ => ์ด์ ๊นŒ์ง€ ์‚ฌ์šฉ๋จ์œผ๋กœ (์„์ฐจ๋งŒ ๋นผ๊ณ ) -1 => ๋ฐ‘์— [i][3]์ด ์žˆ์–ด์„œ
                    sum[i]+=score[i][j];  // 5ํŒ€์˜ ๊ฐ๊ฐ์˜ ์ ์ˆ˜๋ฅผ ๋Œ€์ž… ์‹œ์ผœ์ค€๋‹ค. ์˜ˆ) i=0๋ฒˆ ๋ฐ˜๋ณต ํ•  ์‹œ (์ฒซ ๋ฒˆ์งธ ํŒ€ ๋ฐ˜๋ณต) sum[0]์— ๊ฐ๊ฐ์˜ ์ ์ˆ˜ ๋Œ€์ž…, ๋‹ค์Œ์€ sum[1]์— 2ํŒ€์ ์ˆ˜๋Œ€์ž…
            		temp = sum[i];   //tmep์— ์ด์ ์„ ๋„ฃ์–ด ๋‘”๋‹ค. //์œ„์น˜๋ฅผ ํ•œ ๋ฒˆ๋งŒ์— ๋ฐ”๊พธ์ง€ ๋ชปํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ๋นˆ๊ณต๊ฐ„์ด ์žˆ์–ด์•ผ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค. 
            		sum[i]=score[i][3]; //๋นˆ๊ณต๊ฐ„์ด ๋œ sum์— ์ด์ ์„ ๋„ฃ์–ด์ค€๋‹ค. ์˜ˆ)score[1][i]์€ ๊ฐ๊ฐ score[1][0] = 1์กฐ์˜ ์ด์  ์ž๋ฆฌ 
            		score[i][3]=temp;  //๋นˆ๊ณต๊ฐ„์ด ๋œ score์— ์ด์ ์„ ๋Œ€์ž…ํ•œ๋‹ค. 
                 }
       }
          return score;
       }
       
       public static int[][] getRank(int score[][]){  //์ˆœ์œ„
    	      for(int i = 0; i<score.length; i++) {  //5ํŒ€ 5๋ฒˆ ๋ฐ˜๋ณต 
    	         score[i][4]=1;  //์„์ฐจ์— 1์„ ๊ฐ๊ฐ ๋Œ€์ž…์‹œ์ผœ์ค€๋‹ค. 
    	         for(int j = 0; j<score.length; j++) {  //5๋ฒˆ ๋ฐ˜๋ณต => ์ด์  ์„์ฐจ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ์™„ ์ฐฝ ์ด ์ด ์„(5์—ด)๊นŒ์ง€ ๋‹ค ๋ฐ˜๋ณต์‹œ์ผœ์•ผ ํ•œ๋‹ค.
    	            if(score[i][3]<score[j][3]) { //๋‘ ์กฐ๋ฅผ ๋น„๊ตํ•ด์„œ ๋’ค์— ์žˆ๋Š” ์ˆซ์ž๊ฐ€ ๋” ํฌ๋ฉด ์•ž์กฐ์˜ ์ˆซ์ž๋ฅผ ์˜ฌ๋ ค์ค€๋‹ค.
    	               score[i][4]++; //์ˆœ์œ„ ํ•ญ๋ชฉ์˜ ๋ฐฐ์—ด๋ฒˆํ˜ธ๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ ์ˆœ์œ„๋ฅผ ๋งŒ๋“ ๋‹ค.
    	           } 
    	         }
    	      }
    	      return score;
    	   }
       public static void printResult(int[][] score) { //์ถœ๋ ฅ
    	   int team=0; //์ตœ๊ณ ์กฐ 
    	   int max=0;  //์ตœ๊ณ ์กฐ ์ ์ˆ˜ 
          System.out.println("์กฐ๋ฒˆํ˜ธ\t์™„์„ฑ๋„\t์ฐฝ์˜์„ฑ\t์ดํ•ด๋„\t์ด์ \t์„์ฐจ");
          System.out.println("==============================================");
          
          for(int i=0; i<score.length; i++){ //์กฐ 5๋ฒˆ 
        	  System.out.print((i+1)+"์กฐ"+"\t");   //1-5์กฐ ์ถœ๋ ฅ
             for(int j =0; j<score[i].length; j++) { //5์—ด  
                   System.out.print(score[i][j]+"\t"); //์ „๋ถ€ ์ถœ๋ ฅ
             }  
             System.out.println(""); // ํ•œ ์ค„ ๋„์šฐ๊ธฐ
             
             if(score[i][4]==1) {    //๋“ฑ์ˆ˜๊ฐ€ 1๋“ฑ์ด๋ฉด 
            	team=(i+1);  // i๊ฐ€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•จ์œผ๋กœ i+1์„ ํ•ด์ค€๋‹ค. 
            	max=score[i][3];  //[i][4]๊ฐ€ 1๋“ฑ์ผ๋•Œ ์ด์ ์ธ [i][3]์ด ์ตœ๊ณ ์ ์ด ๋œ๋‹ค.
         	
             }
    
          }
       
        System.out.println("=================================================");
        System.out.println("\n"+"์ตœ๊ณ ํŒ€:"+team+"๋ฒˆ์งธ ํŒ€ "+"์ ์ˆ˜:"+max);
    }
       }