๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java

[Java-๊ธฐ์ดˆ] ์‹ค์Šต (๋กœ๋˜ ์ถ”์ฒจ ๋ฒˆํ˜ธ)

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 24.
๋ฐ˜์‘ํ˜•

์‹ค์Šต ) ๋กœ๋˜ ๋ฒˆํ˜ธ๋ฅผ ๋žœ๋ค์œผ๋กœ ์ถ”์ฒจํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์กฐ๊ฑด : nextInt( ) ๋ฉ”์„œ๋“œ ์‚ฌ์šฉํ•˜๊ธฐ

 

โ€‹

<๋‚ด๊ฐ€ ํ•œ ๊ฒƒ>

โ€‹

1. ์‚ฌ์‹ค ๋กœ๋˜๋ฅผ ์•ˆํ•ด๋ด์„œ ์ž˜ ๋ชฐ๋ž๋‹ค. ์ˆซ์ž๊ฐ€ 45๋ฒˆ๊นŒ์ง€ ์žˆ๋Š”๊ฒƒ๋„;;

Random ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑํ•˜๊ณ  nextInt( ) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค๋Š” ํžŒํŠธ๋ฅผ ๋ฐ›์•˜๋‹ค.

โ€‹

โ€‹

RandomLotto.java

package lotto;

import java.util.Random;

public class RandomLotto {
	public static void main(String[] args) {
		int[] lotto = new int[7]; // ๋กœ๋˜๋ฒˆํ˜ธ 7๊ฐœ
		int cnt = 0; // ์ˆซ์ž ์ƒ์„ฑ
		Random rm = new Random();

		System.out.print("๋กœ๋˜๋ฒˆํ˜ธ ์ƒ์„ฑ : ");

		// ๋กœ๋˜ ๋žœ๋ค ์ถ”์ฒจ
		while (cnt < 7) {
			lotto[cnt] = rm.nextInt(45) + 1; // ๋กœ๋˜๋ฒˆํ˜ธ๋Š” 45๋ฒˆ๊นŒ์ง€ ์žˆ์Œ. 0์ด ์•ˆ๋‚˜์˜ค๊ฒŒํ•˜๋ ค๋ฉด + 1 ๋„ฃ์–ด์ค˜์•ผํ•จ
			for (int i = 0; i < cnt; i++) {
				if (lotto[i] == lotto[cnt]) { // ์ค‘๋ณต๋œ ์ˆซ์ž๊ฐ€ ์žˆ์œผ๋ฉด
					cnt--; // ์ค‘๋ณต๋œ ์ˆซ์ž ์ œ๊ฑฐ
					continue; // while๋ฌธ์˜ ์ฒ˜์Œ์œผ๋กœ ๋Œ์•„๊ฐ€ ์ฆ๊ฐ์‹์„ ์ˆ˜ํ–‰ํ•จ
				}
			}
			cnt++; // ๊ณ„์† ์ถ”์ฒจ
		}

		// ์ถœ๋ ฅ๋ฌธ
		for (int i = 0; i < lotto.length; i++) {
			System.out.print(lotto[i] + " ");
		}
	}
}

<๊ฒฐ๊ณผ๊ฐ’>

โ€‹

โ€‹

2. ๋กœ๋˜์— ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๊ฐ€ ์žˆ๋Š”์ค„ ๋ชฐ๋ž๋‹ค ใ…‹ใ…‹ใ…‹

๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋Š” ๋”ฐ๋กœ ์ถœ๋ ฅ๋˜๊ฒŒ ๋งŒ๋“ฆ.

โ€‹

โ€‹

RandomLotto.java

package lotto;

import java.util.Random;

public class RandomLotto {
	public static void main(String[] args) {
		int[] lotto = new int[7]; // ๋กœ๋˜๋ฒˆํ˜ธ 7๊ฐœ
		int cnt = 0; // ์ˆซ์ž ์ƒ์„ฑ
		Random rm = new Random();

		System.out.print("๋กœ๋˜๋ฒˆํ˜ธ ์ƒ์„ฑ : ");

		// ๋กœ๋˜ ๋žœ๋ค ์ถ”์ฒจ
		while (cnt < 7) {
			lotto[cnt] = rm.nextInt(45) + 1; // ๋กœ๋˜๋ฒˆํ˜ธ๋Š” 45๋ฒˆ๊นŒ์ง€ ์žˆ์Œ. 0์ด ์•ˆ๋‚˜์˜ค๊ฒŒํ•˜๋ ค๋ฉด + 1 ๋„ฃ์–ด์ค˜์•ผํ•จ
			for (int i = 0; i < cnt; i++) {
				if (lotto[i] == lotto[cnt]) { // ์ค‘๋ณต๋œ ์ˆซ์ž๊ฐ€ ์žˆ์œผ๋ฉด
					cnt--; // ์ค‘๋ณต๋œ ์ˆซ์ž ์ œ๊ฑฐ
					continue; // while๋ฌธ์˜ ์ฒ˜์Œ์œผ๋กœ ๋Œ์•„๊ฐ€ ์ฆ๊ฐ์‹์„ ์ˆ˜ํ–‰ํ•จ
				}
			}
			cnt++; // ๊ณ„์† ์ถ”์ฒจ
		}

		// ์ถœ๋ ฅ๋ฌธ
		for (int i = 0; i < lotto.length; i++) {
			if (i == lotto.length - 1) // ๋ฐฐ์—ด ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„์— ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ ์ถœ๋ ฅ
				System.out.println("๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ : " + lotto[i]);
			else
				System.out.print(lotto[i] + " ");
		}
	}
}

<๊ฒฐ๊ณผ๊ฐ’>

โ€‹

๋”๋ณด๊ธฐ

<์„ ์ƒ๋‹˜์ด ํ•œ ๊ฒƒ>

 

โ€‹โ‘  while๋ฌธ, continue๋ฌธ ์‚ฌ์šฉ

 

lotto1.java

package lotto;

import java.util.Random;

public class lotto1 {
	public static void main(String[] args) {
		Random random = new Random(); // ๋žœ๋ค ๋ฐœ์ƒ ํด๋ž˜์Šค
		int[] lotto = new int[7]; // ๋กœ๋˜๋ฒˆํ˜ธ๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด
		int getnum; // ๋žœ๋ค๊ฐ์ฒด๋กœ ๋ฐœ์ƒํ•œ ๊ฐ’ ์ €์žฅ ๋ณ€์ˆ˜
		int i = 0;

		// ๋กœ๋˜ ๋ฒˆํ˜ธ ๋žœ๋ค ์ถ”์ฒจ
		It: while (i < lotto.length) {
			getnum = random.nextInt(45) + 1; // ๋žœ๋ค ๊ฐ’์„ ๋ณ€์ˆ˜์— ์ €์žฅ
			System.out.println(i + ":" + getnum); // ๋žœ๋ค ๊ฐ’ ํ™•์ธ
			for (int j = 0; j < i; j++) {
				if (getnum == lotto[j]) { // ๋žœ๋ค ๊ฐ’์ด ์ €์žฅ๋œ ๋กœ๋˜๊ฐ’ ๋น„๊ตํ•ด์„œ ๊ฐ™์œผ๋ฉด
					continue It; // while ๋ฐ˜๋ณต๋ฌธ๋ถ€ํ„ฐ ์‹คํ–‰
				}
			}
			lotto[i] = getnum; // ์ค‘๋ณต๋œ ๊ฐ’์ด ์—†์œผ๋ฏ€๋กœ ๋ฐฐ์—ด์— ์ €์žฅ
			i++;
		}

		// ์ถœ๋ ฅ๋ฌธ
		for (int j = 0; j < lotto.length; j++) {
			if (j == lotto.length - 1)
				System.out.println("๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ : " + lotto[j]);
			else
				System.out.print(lotto[j] + " ");
		}
	}
}

<๊ฒฐ๊ณผ๊ฐ’>

โ€‹โ€‹โ€‹

โ‘ก for๋ฌธ, break๋ฌธ ์‚ฌ์šฉ

โ€‹

lotto2.java

package lotto;

import java.util.Random;

public class lotto2 {
	public static void main(String[] args) {
		Random random = new Random(); // ๋žœ๋ค ๋ฐœ์ƒ ํด๋ž˜์Šค
		int[] lotto = new int[7]; // ๋กœ๋˜ ๋ฒˆํ˜ธ๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด

		//๋กœ๋˜ ๋ณ€์ˆ˜ ๋žœ๋ค ์ถ”์ฒจ
		for (int i = 0; i < lotto.length; i++) {
			lotto[i] = random.nextInt(45) + 1; // ๋ฐฐ์—ด์— ๋žœ๋ค ๊ฐ’ ์ €์žฅ
			System.out.println(i + ":" + lotto[i]); // ์ธ๋ฑ์Šค ์œ„์น˜์™€ ๋žœ๋ค๊ฐ’ ํ™•์ธ
			for (int j = 0; j < i; j++) {
				if (lotto[i] == lotto[j]) { // ์ƒˆ๋กœ ์ €์žฅํ•œ ๊ฐ’ [i]์™€ ์ €์žฅ๋œ ๊ฐ’ [j]
					i--; // ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ ๊ฐ์†Œํ•˜์—ฌ ์ƒˆ๋กœ ์ €์žฅ๋œ ๊ฐ’์„ ๋‹ค์‹œ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค
					break; // ์ค‘๋ณต ํ™•์ธ ๋˜์—ˆ์œผ๋ฏ€๋กœ ์ค‘๋ณต ๊ฒ€์‚ฌ ๋ฐ˜๋ณต์„ ๋ฒ—์–ด๋‚œ๋‹ค
				}
			}
		}

		// ์ถœ๋ ฅ๋ฌธ
		for (int j = 0; j < lotto.length; j++) {
			if (j == lotto.length - 1)
				System.out.println("๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ : " + lotto[j]);
			else
				System.out.print(lotto[j] + " ");
		}
	}
}

โ€‹<๊ฒฐ๊ณผ๊ฐ’>

 

โ€‹

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€