Programming Language/Java

Java 3์ผ์ฐจ ๊ณต๋ถ€(์กฐ๊ฑด๋ฌธ, ๋ฐ˜๋ณต๋ฌธ)

chaerlo127 2022. 1. 29. 18:29
728x90

1. Math.random() * n

Math.random() * n ์—์„œ, (int) Math.random() * 10์€ 1์—์„œ 10๊นŒ์ง€์˜ ์ •์ˆ˜ ์ค‘์—์„œ ๋žœ๋คํ•œ ์ˆซ์ž๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. 

public static void main(String args[]) {
	System.out.println("๋กœ๋˜ ๋ฒˆํ˜ธ ์ƒ์„ฑ ํ”„๋กœ๊ทธ๋žจ");
	for(int i=0; i<6 ; i ++) {
		System.out.println( (i+1) + "๋ฒˆ์งธ ๋กœ๋˜ ๋ฒˆํ˜ธ: " + (int)(Math.random() * 45));
	}

}

 

2. switch ๋ฌธ

public static void main(String[] args) {
	System.out.println("์ฃผ์‹œ์œ„ ๋˜์ง€๊ธฐ");
	int a = (int) (Math.random() * 6);
		
	switch(a) {
	case 1:
		System.out.println("์ฃผ์‚ฌ์œ„ 1๋ฒˆ");
		break;
	case 2:
		System.out.println("์ฃผ์‚ฌ์œ„ 2๋ฒˆ");
		break;
	case 3:
		System.out.println("์ฃผ์‚ฌ์œ„ 3๋ฒˆ");
		break;
	case 4:
		System.out.println("์ฃผ์‚ฌ์œ„ 4๋ฒˆ");
		break;
	case 5:
		System.out.println("์ฃผ์‚ฌ์œ„ 5๋ฒˆ");
		break;
	default:
		System.out.println("์ฃผ์‚ฌ์œ„ 6๋ฒˆ");
		break;
	}
}

 

3. for ๋ฌธ

๊ตฌ๊ตฌ๋‹จ ์˜ˆ์‹œ

public static void main(String args[]) {
		for(int i = 1; i < 10 ; i++) {
			System.out.println("๊ตฌ๊ตฌ๋‹จ: " + i + "๋‹จ");
			for(int j = 1 ; j < 10 ; j ++)
				System.out.println(i+"*"+j+"="+(i*j));
		}
	}
}

 

4. do-while ๋ฌธ

์ผ๋‹จ while๋ฌธ์„ ์‹คํ–‰ํ•˜๊ณ , ๊ทธ ํ›„ ์กฐ๊ฑด ์‹์„ ํ™•์ธํ•˜์—ฌ ์กฐ๊ฑด์‹์ด false ๋ฉด while ๋ฌธ์„ ๋” ์ด์ƒ ์‹คํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค.

public static void main(String args[]) {
	Scanner sc = new Scanner(System.in);
	int a;
	do {
		System.out.println("1~10 ์ค‘์— ํ•œ ๊ฐœ๋งŒ ์„ ํƒํ•˜์„ธ์š”. ๊ทธ ์ค‘์—์„œ ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•  ์ˆ˜ ์žˆ๋Š” ์ˆซ์ž๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.");
		a = sc.nextInt();
	}while(a != 1);
}

 

5. ๋ฐ”๊นฅ์ชฝ ๋ฐ˜๋ณต๋ฌธ ์ข…๋ฃŒ

for ๋ฌธ ์˜†์— ์ข…๋ฃŒํ•  ๋•Œ ์‚ฌ์šฉํ•˜๊ณ ์žํ•˜๋Š” ์ด๋ฆ„์„ ์ ๊ณ , ํ›„์— ๋ฐ˜๋ณต๋ฌธ์„ ์‹คํ–‰ํ•˜๋‹ค ๊ทธ๋งŒ๋‘ฌ์•ผํ•˜๋Š” ์‹œ์ ์— "break + for๋ฌธ Name"์„ ์ž‘์„ฑํ•œ๋‹ค.

public static void main(String args[]) {
	ABC : for(int i= 0 ; i <10 ; i++) {
		System.out.println(i);
		if(i==8) {
			break ABC;
		}
	}
}

 

6. Continue

continue๋ฌธ์ด ์‹คํ–‰๋˜๋ฉด, for, while, do-while์˜ ์กฐ๊ฑด์‹์œผ๋กœ ์ด๋™ํ•œ๋‹ค. ์ฆ‰, ์ฝ”๋“œ ๋ฐ‘์œผ๋กœ ๋‚ด๋ ค๊ฐ€์ง€ ์•Š๊ณ , ๋‹ค์Œ ์ฐจ๋ก€์˜ ์กฐ๊ฑด ์‹์„ ์‹œํ–‰ํ•œ๋‹ค.

public static void main(String[] args) {
	for(int i = 0; i < 11 ; i++) {
		if(i%2==0) { //์ง์ˆ˜์ผ ๋•Œ
			continue; // println ์‹คํ–‰ํ•˜์ง€ ์•Š๊ณ , ๋‹ค์‹œ for ๋ฌธ์œผ๋กœ ์ด๋™
		}
		System.out.println(i);
	}

}

 

7. ์€ํ–‰ ์ž”๊ณ  ํ”„๋กœ๊ทธ๋žจ ์˜ˆ์‹œ

public static void main(String[] args) {
		boolean run = true;
		int balance = 0;
		Scanner sc = new Scanner(System.in);
		while(run) {
			
			System.out.println("------------------------------------");
			System.out.println("1. ์˜ˆ๊ธˆ | 2. ์ถœ๊ธˆ | 3. ์ž”๊ณ  | 4. ์ข…๋ฃŒ");
			System.out.print("์„ ํƒ> ");
			String a = sc.next();
			if(a.equals("1")) {
				System.out.println("์˜ˆ๊ธˆ์•ก> ");
				int money = sc.nextInt();
				balance +=money;
				
			}else if(a.equals("2")) {
				System.out.println("์ถœ๊ธˆ์•ก> ");
				int money = sc.nextInt();
				balance -=money;
				
			}else if(a.equals("3")) {
				System.out.println("์ž”๊ณ > " + balance);
				
			}else if(a.equals("4")) {
				System.exit(0);
			}
			System.out.println();
	
		}
		sc.close();
		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ");
	}

์—ฌ๊ธฐ์—์„œ์˜ ๋ฌธ์ œ์ ์€ next๊ฐ€ ์•„๋‹Œ nextLine์„ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ, ์ค‘๋ณต๋˜์–ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ˜์†”์ด ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.

 

๋”ฐ๋ผ์„œ, nextLine์ด ์•„๋‹Œ next๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

728x90