[Java] Solution to puzzles

Joy๐ŸŒฑยท2022๋…„ 12์›” 22์ผ
0

๐Ÿงฉ Coding Challenges

๋ชฉ๋ก ๋ณด๊ธฐ
2/20
post-thumbnail
post-custom-banner

๐Ÿงฉ random method๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ๋งŒ๋“  ํ›„, ๋‹ค๋ฅธ ํด๋ž˜์Šค์—์„œ call ํ•˜๊ธฐ

โ—ผ Application Class

package com.greedy.level02.normal;

public class Application {

	public static void main(String[] args) {
		
		int min = -50;
		int max = 50;
		
		// ์ตœ์†Œ๊ฐ’๋ถ€ํ„ฐ ์ตœ๋Œ€๊ฐ’๊นŒ์ง€ ๋ฒ”์œ„์˜ ๋‚œ์ˆ˜ ๋ฐ˜ํ™˜ํ•จ
		System.out.println(RandomMarker.randomNumber(min, max));
		
		int len = 10;
		
		// ์ธ์ž์˜ ๊ธธ์ด ๋งŒํผ์˜ ๋žœ๋คํ•œ ๋Œ€๋ฌธ์ž ์•ŒํŒŒ๋ฒณ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•จ
		System.out.println(RandomMarker.randomUpperAlphabet(len));
		
		// ๊ฐ€์œ„, ๋ฐ”์œ„, ๋ณด ์ค‘ ํ•œ ๊ฐ€์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•จ
		System.out.println(RandomMarker.rockPaperScissors());
		
		// ๋™์ „์˜ ์•ž๋ฉด, ๋’ท๋ฉด ์ค‘ ํ•œ ๊ฐ€์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•จ
		System.out.println(RandomMarker.tossCoin());
	}
}

๐Ÿ“Œ Ref.

* callํ•  method๊ฐ€ static method์ด๋ฏ€๋กœ ๋”ฐ๋กœ ๊ฐ์ฒด๋Š” ์ƒ์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋จ
* min, max, len๋“ฑ ๋ณ€์ˆ˜๋ฅผ ๋”ฐ๋กœ ์ง€์ •ํ•˜์ง€์•Š๊ณ  ์ง์ ‘ ์‹์— ๊ฐ’์„ ๋„ฃ์–ด๋„ ๋จ

โ—ผ RandomMarker Class

package com.greedy.level02.normal;

import java.util.Random;
>>> random import ๊ตฌ๋ฌธ

public class RandomMarker {
	
	static Random random = new Random();
	>>> random ๊ฐ์ฒด ์ƒ์„ฑ
	
	public static int randomNumber(int min, int max) { 
		
		int num1 = (max - min) + 1;
		int result1 = random.nextInt(num1) + min;
		>>> num1 : ์ž…๋ ฅ๋˜๋Š” (max - min) + 1์€ ์ตœ๋Œ€๊ฐ’๊ณผ ์ตœ์†Œ๊ฐ’ ์‚ฌ์ด์˜ ๋ฒ”์œ„
		>>> result1 : ์‹œ์ž‘์ (min)๋ถ€ํ„ฐ ์ง€์ •ํ•œ ๋ฒ”์œ„(num1)๊นŒ์ง€์˜ ๋žœ๋คํ•œ ์ˆ˜ 
				
		return result1;
		>>> return ๊ฐ’์€ ๋ฉ”์†Œ๋“œ์˜ ์ž๋ฃŒํ˜•์ด ์–ด๋–ค๊ฑด์ง€์— ๋”ฐ๋ผ ์ขŒ์šฐ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๊ณณ์—์„  int ๊ฐ’์„ ๋„ฃ๊ธฐ 
	}
		
	public static String randomUpperAlphabet(int length) { 
	
		String upperRandom = "";
		>>> return ๊ฐ’์€ String์ด์–ด์•ผํ•˜๋ฏ€๋กœ ๋ณ€์ˆ˜์ง€์ •
        
		for (int i = 1 ; i <= length ; i++) {
			int upperUni = (90 - 65) + 1;
			char upperCh1 = (char)(random.nextInt(upperUni)+65);
			upperRandom += upperCh1;
			>>> i๋Š” ์ž…๋ ฅ๋  length๋งŒํผ ์ฆ๊ฐ€ํ•  ๊ฒƒ
			>>> uppperUni : (Z์˜ ์œ ๋‹ˆ์ฝ”๋“œ - A์˜ ์œ ๋‹ˆ์ฝ”๋“œ) + 1 ์€ ์ตœ๋Œ€๊ฐ’๊ณผ ์ตœ์†Œ๊ฐ’ ์‚ฌ์ด์˜ ๋ฒ”์œ„
			>>> upperCh1 : ์‹œ์ž‘์ (A-65)๋ถ€ํ„ฐ ์ง€์ •ํ•œ ๋ฒ”์œ„๊นŒ์ง€์˜ ๋žœ๋คํ•œ ์ˆ˜๋ฅผ (char)๋กœ ๊ฐ•์ œ ํ˜•๋ณ€ํ™˜
			>>> uppperRandom : upperRandom = upperRandom + upperCh1 
			>>> ์ฆ‰, ๋ฌธ์ž๋กœ ๊ฐ•์ œ ํ˜•๋ณ€ํ™˜๋œ upperCh1์ด ์ž…๋ ฅ๋œ ์ •์ˆ˜์ธ length๋งŒํผ์˜ ๊ฐฏ์ˆ˜๊ฐ€ ๋žœ๋ค์œผ๋กœ ์ถœ๋ ฅ๋จ
			>>> ๊ทธ๊ฒƒ์€ upperRandom์— ์Œ“์—ฌ ์ €์žฅ๋˜๊ณ  return ๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜๋จ
		}
		
		return upperRandom;
	
	}
	
	public static String rockPaperScissors() {  
		
		int num2 = random.nextInt(3);
		String result2 = num2 == 0 ? "๊ฐ€์œ„" : (num2 == 1 ? "๋ฐ”์œ„" : "๋ณด");
		>>> num2 : 0, 1, 2 ์ˆซ์ž ์ค‘ ๋žœ๋ค 
		>>> result2 : num2๊ฐ€ 0์ด๋ฉด ๊ฐ€์œ„, ๊ทธ๊ฒŒ ์•„๋‹ˆ๊ณ  1์ด๋ฉด ๋ฐ”์œ„, ๊ทธ๊ฒƒ๋„ ์•„๋‹ˆ๋ฉด ๋ณด */
		
		return result2;
	}
	
	public static String tossCoin() { 
		
		int num3 = random.nextInt(2);
		String result3 = num3 == 0 ? "์•ž๋ฉด" : "๋’ท๋ฉด";
		>>> num3 : 0, 1 ์ˆซ์ž ์ค‘ ๋žœ๋ค 
		>>> result3 : num3์ด 0์ด๋ฉด ์•ž๋ฉด, ์•„๋‹ˆ๋ฉด ๋’ท๋ฉด */
		
		return result3;
	}
}

๐Ÿ“Œ Ref.

* static Random random = new Random(); ์œผ๋กœ random๊ฐ์ฒด ์ƒ์„ฑํ•˜์—ฌ random.next ์‚ฌ์šฉ
* 'shift + ctrl + O' ๋‹จ์ถ•ํ‚ค๋กœ random import๊ตฌ๋ฌธ ์ƒ์„ฑ
* return ๊ฐ’์€ method์˜ ์ž๋ฃŒํ˜•์ด ๋ฌด์—‡์ธ์ง€์— ๋”ฐ๋ผ ์ขŒ์šฐ๋˜๊ธฐ ๋•Œ๋ฌธ์— method์˜ ์ž๋ฃŒํ˜•๊ณผ 
  ๋™์ผํ•œ ๊ฐ’์„ ๋„ฃ์–ด์•ผํ•จ
profile
Tiny little habits make me
post-custom-banner

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