22.03.23_์กฐ๊ฑด๋ฌธ_switch(2)

public static void main(String[] args) {
		System.out.print("๊ฐ€์œ„(1), ๋ฐ”์œ„(2), ๋ณด(3) ์ค‘ ํ•˜๋‚˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.>");
		
		Scanner scanner = new Scanner(System.in);
		int user = scanner.nextInt();			// ํ™”๋ฉด์„ ํ†ตํ•ด ์ž…๋ ฅ๋ฐ›์€ ์ˆซ์ž๋ฅผ user์— ์ €์žฅ
		int com = (int)(Math.random()*3) + 1; 	// 1,2,3 ์ค‘ ํ•˜๋‚˜๊ฐ€ com์— ์ €์žฅ๋จ
		
		System.out.println("๋‹น์‹ ์€ " + user + "์ž…๋‹ˆ๋‹ค.");
		System.out.println("์ปด์€ " + com + "์ž…๋‹ˆ๋‹ค.");
		
		switch (user - com) {
			case -1: case 2:
				System.out.println("๋‹น์‹ ์ด ์กŒ์Šต๋‹ˆ๋‹ค");
				break;
			case 1: case -2:
				System.out.println("๋‹น์‹ ์ด ์ด๊ฒผ์Šต๋‹ˆ๋‹ค");
				break;
			case 0:
				System.out.println("๋น„๊ฒผ์Šต๋‹ˆ๋‹ค");
//				break;				// ๋งˆ์ง€๋ง‰ ๋ฌธ์žฅ์ด๋ฏ€๋กœ break๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๋Š” ์—†๋‹ค.
		}
	} // main์˜ ๋

Math.random() : 0~1 ์‚ฌ์ด์— ํ•˜๋‚˜์˜ double ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

Math.random()์€ double๋กœ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ •์ˆ˜๋กœ ํ˜•๋ณ€ํ™˜์„ ํ•ด์ค˜์•ผํ•œ๋‹ค.

ํ•ด๋‹น ๋งค์„œ๋“œ์— ์–ด๋–ค ์ˆซ์ž๋ฅผ ๊ณฑํ•˜๋Š๋ƒ์— ๋”ฐ๋ผ ๋ฌด์ž‘์œ„ ์ •์ˆ˜์˜ ๋ฒ”์œ„๋ฅผ ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
์˜ˆ๋ฅผ๋“ค์–ด 6์„ ๊ณฑํ•œ๋‹ค๋ฉด ์ฃผ์‚ฌ์œ„ ๊ฐ’์„ ๋žœ๋ค์œผ๋กœ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค!

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ