๐Ÿ“˜ 221016 JAVA ๋ณต์Šต์ผ์ง€1

rossiebiยท2022๋…„ 10์›” 16์ผ
0

๐Ÿ’ป JAVA ๊ฐœ๋ฐœ์ผ์ง€ย 

๋ชฉ๋ก ๋ณด๊ธฐ
7/38
post-thumbnail

๐Ÿ”” [ ์กฐ๊ฑด๋ฌธ๊ณผ ๋ฐ˜๋ณต๋ฌธ ]

๐Ÿ“ข ์กฐ๊ฑด๋ฌธ : ์กฐ๊ฑด์„ ๋งŒ์กฑํ• ๋•Œ๋งŒ {}๋ฅผ ์ˆ˜ํ–‰ (0~1๋ฒˆ)

๐Ÿ“ข ๋ฐ˜๋ณต๋ฌธ : ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋™์•ˆ {}๋ฅผ ์ˆ˜ํ–‰(0~n๋ฒˆ)

๐Ÿ”” [ if๋ฌธ ]

1) if๋ฌธ
 *   ํ˜•์‹1)
 *   if(์กฐ๊ฑด์‹){
 *   	์กฐ๊ฑด์ด ์ฐธ์ผ๋•Œ ์ˆ˜ํ–‰ํ•  ๋ฌธ์žฅ;
 *   	..
 *   }
 

๐Ÿ“Œ ์˜ˆ์ œ1. 60์  ์ด์ƒ์ด๋ฉด ํ•ฉ๊ฒฉ


// 60์  ์ด์ƒ์ด๋ฉด ํ•ฉ๊ฒฉ (if๋ฌธ)
public class Practice_1 {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("์ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”");
		int score = scan.nextInt();
		if (score > 60) {
			System.out.println("ํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค");
		}

	}

}

๐Ÿ“ข if-else๋ฌธ : ๋‘˜ ์ค‘์˜ ํ•˜๋‚˜ - ์กฐ๊ฑด์‹์ด ์ฐธ์ผ๋•Œ์™€ ๊ฑฐ์ง“์ผ๋•Œ๋กœ ๋‚˜๋ˆ ์„œ ์ฒ˜๋ฆฌ

 ํ˜•์‹2)
 *   if(์กฐ๊ฑด์‹){
 *   	์กฐ๊ฑด์ด ์ฐธ์ผ๋•Œ ์ˆ˜ํ–‰ํ•  ๋ฌธ์žฅ;
 *   	..
 *   }else{
 *   	์กฐ๊ฑด์ด ๊ฑฐ์ง“์ผ๋•Œ ์ˆ˜ํ–‰ํ•  ๋ฌธ์žฅ;
 *   }

๐Ÿ“ข if-else-if๋ฌธ : ์—ฌ๋Ÿฌ ๊ฐœ ์ค‘์˜ ํ•˜๋‚˜ - ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์กฐ๊ฑด์‹์„ ํฌํ•จํ•œ ์กฐ๊ฑด์‹

ํ˜•์‹3)
 *   if(์กฐ๊ฑด์‹1){
 *   	์กฐ๊ฑด์‹1์ด ์ฐธ์ผ๋•Œ ์ˆ˜ํ–‰ํ•  ๋ฌธ์žฅ;
 *   	..
 *   }else if(์กฐ๊ฑด์‹2){
 *   	์กฐ๊ฑด์‹2์ด ์ฐธ์ผ๋•Œ ์ˆ˜ํ–‰ํ•  ๋ฌธ์žฅ; // ์—ฌ๋Ÿฌ๊ฐœ์˜ else if ์‚ฌ์šฉ ๊ฐ€๋Šฅ
 *   }
 *   ..
 *   else{
 *   	์กฐ๊ฑด์ด ๋ชจ๋‘ ๊ฑฐ์ง“์ผ๋•Œ ์ˆ˜ํ–‰ํ•  ๋ฌธ์žฅ
 *   }

๐Ÿ“Œ ์˜ˆ์ œ2. ์ ์ˆ˜ ์ž…๋ ฅ๋ฐ›์•„ ํ•™์  ๊ตฌํ•˜๊ธฐ

import java.util.Scanner;

// ์ ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ํ•™์ ์„ ์•Œ์•„๋ณด์‹œ์˜ค 
public class Practice_10 {

	public static void main(String[] args) {
		int score = 0;
		char grade = ' ';
	Scanner scan = new Scanner (System.in);
	System.out.println("์ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”");
	score = scan.nextInt();
	
	if(score>=90) {
		grade = 'A';
	}else if(score>=80) {
		grade = 'B';
	}else if(score >= 70) {
		grade = 'C';
	}else {
		grade='D';
	}
	System.out.println("ํ•™์ ์€ :" + grade);
	}

}

๐Ÿ“ข ์ค‘์ฒฉ if๋ฌธ - if๋ฌธ ์•ˆ์˜ if

if(์กฐ๊ฑด์‹1){
	if(์กฐ๊ฑด์‹2){
    
    }else{
    
    }
    
   }else{
   
   }

๐Ÿ”” [ switch๋ฌธ ]

๐Ÿ“ข switch๋ฌธ : ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋งŽ์„ ๋•Œ ์œ ์šฉํ•œ ์กฐ๊ฑด๋ฌธ

switch (์กฐ๊ฑด์‹) { 
 case ๊ฐ’1 : 
 // ์กฐ๊ฑด์‹์˜ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ’1๊ณผ ๊ฐ™์„ ๊ฒฝ์šฐ ์ˆ˜ํ–‰๋  ๋ฌธ์žฅ๋“ค
 //... 
 break; 
 case ๊ฐ’2 : 
 // ์กฐ๊ฑด์‹์˜ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ’2์™€ ๊ฐ™์„ ๊ฒฝ์šฐ ์ˆ˜ํ–‰๋  ๋ฌธ์žฅ๋“ค
 //... 
 break; // switch๋ฌธ์„ ๋ฒ—์–ด๋‚œ๋‹ค.
 //... 
 default : 
 // ์กฐ๊ฑด์‹์˜ ๊ฒฐ๊ณผ์™€ ์ผ์น˜ํ•˜๋Š” case๋ฌธ์ด ์—†์„ ๋•Œ ์ˆ˜ํ–‰๋  ๋ฌธ์žฅ๋“ค
 //... 
}

๐Ÿ“Œ ์˜ˆ์ œ : ์›”์„ ์ž…๋ ฅ๋ฐ›์•„ ๊ณ„์ ˆ ์•Œ์•„๋ณด๊ธฐ

import java.util.Scanner;

// ์›”์„ ์ž…๋ ฅ๋ฐ›์•„ ๊ณ„์ ˆ์„ ์•Œ์•„๋ณด๊ธฐ (switch)
public class Practice_11 {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("ํ˜„์žฌ ์›”์„ ์ž…๋ ฅํ•˜์„ธ์š”");
		int month = scan.nextInt();
		
		switch(month) {
		case 3:
		case 4:
		case 5:
			System.out.println("ํ˜„์žฌ ๊ณ„์ ˆ์€ ๋ด„์ž…๋‹ˆ๋‹ค.");
			break;
		case 6:
		case 7:
		case 8:
			System.out.println("ํ˜„์žฌ ๊ณ„์ ˆ์€ ์—ฌ๋ฆ„์ž…๋‹ˆ๋‹ค.");
			break;
		case 9:
		case 10:
		case 11:
			System.out.println("ํ˜„์žฌ ๊ณ„์ ˆ์€ ๊ฐ€์„์ž…๋‹ˆ๋‹ค.");
			break;
			default:
//		case 12:
//		case 1:
//		case 2:
			System.out.println("ํ˜„์žฌ ๊ณ„์ ˆ์€ ๊ฒจ์šธ์ž…๋‹ˆ๋‹ค.");
		
			
		}

	}

}

๐Ÿ“Œ ์˜ˆ์ œ : 1~10 ์‚ฌ์ด์˜ ๋‚œ์ˆ˜ 10๊ฐœ ์ถœ๋ ฅํ•˜๊ธฐ

// 1๋ถ€ํ„ฐ 10์‚ฌ์ด์˜ ๋‚œ์ˆ˜๋ฅผ 20๊ฐœ ์ถœ๋ ฅํ•˜์„ธ์š”
public class Practice_12 {

	public static void main(String[] args) {
		for(int i = 1; i <=10; i++) {
//			System.out.println(Math.random()); //0.0~1.0 ์‚ฌ์ด
//			System.out.println(Math.random()*10); // 0์—์„œ 10์‚ฌ์ด
			System.out.println((int)(Math.random()*10)); // ํ˜•๋ณ€ํ™˜
		}

	}

}

๐Ÿ”” [ for๋ฌธ ]

๐Ÿ“ข for๋ฌธ : ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋™์•ˆ ๋ธ”๋Ÿญ์„ ๋ฐ˜๋ณต - ๋ฐ˜๋ณตํšŸ์ˆ˜๋ฅผ ์•Œ ๋•Œ ์ ํ•ฉ

for(int i=1;i<=5;i++) {
 System.out.println("I can do it.");
}

๐Ÿ“Œ ์˜ˆ์ œ : for๋ฌธ ์‚ฌ์šฉํ•ด์„œ hello 3๋ฒˆ ์ถœ๋ ฅํ•˜๊ธฐ

public class Practice_13 {

	public static void main(String[] args) {
	for(int i=1; i<=3; i++) {
		System.out.println("hello");
	}
	}

}

๐Ÿ“ข ์ค‘์ฒฉ for๋ฌธ : for๋ฌธ ๋‚ด์— ๋˜ ๋‹ค๋ฅธ for๋ฌธ

๐Ÿ“Œ ์˜ˆ์ œ : ๊ตฌ๊ตฌ๋‹จ

public class Practice_14 {

	public static void main(String[] args) {
		for(int i=1; i<=9; i++) {
			for(int j=1; j<=9; j++) {
				System.out.println(i + "*" + j + "=" + (i*j));
			}
			System.out.println();
		}

	}

}

๐Ÿ”” [ while๋ฌธ ]

๐Ÿ“ข while๋ฌธ : ์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œํ‚ค๋Š” ๋™์•ˆ ๋ธ”๋Ÿญ์„ ๋ฐ˜๋ณต - ๋ฐ˜๋ณตํšŸ์ˆ˜ ๋ชจ๋ฅผ๋•Œ

while (์กฐ๊ฑด์‹) { 
 // ์กฐ๊ฑด์‹์˜ ์—ฐ์‚ฐ๊ฒฐ๊ณผ๊ฐ€ ์ฐธ(true)์ธ ๋™์•ˆ, ๋ฐ˜๋ณต๋  ๋ฌธ์žฅ๋“ค์„ ์ ๋Š”๋‹ค.
}

๐Ÿ“ข continue๋ฌธ : ์ž์‹ ์ด ํฌํ•จ๋œ ๋ฐ˜๋ณต๋ฌธ์˜ ๋์œผ๋กœ ์ด๋™ - ๋‹ค์Œ ๋ฐ˜๋ณต์œผ๋กœ ๋„˜์–ด๊ฐ - ์ „์ฒด ๋ฐ˜๋ณต ์ค‘์—์„œ ํŠน์ • ์กฐ๊ฑด์‹œ ๋ฐ˜๋ณต์„ ๊ฑด๋„ˆ๋›ธ ๋•Œ ์œ ์šฉ

๐Ÿ“Œ ์˜ˆ์ œ : 3์˜ ๋ฐฐ์ˆ˜๋Š” ๊ฑด๋„ˆ๋›ฐ๊ณ  ์ถœ๋ ฅ

package review_sun;

public class Practice_9 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		for(int i=0; i<=10; i++) {
			if(i%3==0)
				continue;
			System.out.println(i);
		}
	}

}

๐Ÿ”” [ ๋ฐฐ์—ด ]

๐Ÿ“ข ๋ฐฐ์—ด : ๋ฐฐ์—ด์€ ๊ฐ™์€ ํƒ€์ž…์˜ ์—ฌ๋Ÿฌ ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜์˜ ๋ฌถ์Œ์œผ๋กœ ๋‹ค๋ฃจ๋Š” ๊ฒƒ

๐Ÿ“ข ๋ฐฐ์—ด์˜ ์„ ์–ธ๊ณผ ์ƒ์„ฑ

๋ฐฐ์—ด์˜ ์„ ์–ธ : ๋ฐฐ์—ด์„ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•œ ์ฐธ์กฐ๋ณ€์ˆ˜์˜ ์„ ์–ธ

๐Ÿ“Œ ์˜ˆ์ œ : ๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ

public class Practice_18 {

	public static void main(String[] args) {
		int [] score; //๋ฐฐ์—ด score์„ ์„ ์–ธ
		score = new int[5]; //๋ฐฐ์—ด์˜ ์ƒ์„ฑ
		score[3] = 100;
		System.out.println("score[0]="+score[0]);
		System.out.println("score[1]="+score[1]);
		System.out.println("score[2]="+score[2]);
		System.out.println("score[3]="+score[3]);
		System.out.println("score[4]="+score[4]);

	}

}

๐Ÿ“ข ๋ฐฐ์—ด์˜ ๊ธธ์ด : ๋ฐฐ์—ด์ด๋ฆ„.length

ex) int tmp = arr.length;

profile
๋น„์ „๊ณต์ž ๊ฐœ๋ฐœ์ž๋ฅผ ๊ฟˆ๊พธ๋Š” ๋กœ์ง€

0๊ฐœ์˜ ๋Œ“๊ธ€