개발자가 되기까지 (2023.08.16~2024.04.15)/[Basic] Java2023. 9. 8. 21:52[Java] 주사위 게임 (무한루프 while)

주사위는 1~6 사이의 수를 랜덤으로 주고 총 30칸을 이동하면 완주 public class 주사위게임 { public static void main(String[] args) { int goal = 30; int count = 0; // 무한 루프를 주고 while (true) { int dice = (int) (Math.random() * 6) + 1; goal -= dice; count++; System.out.println("주사위 : " + dice); System.out.println(dice + "칸 이동"); System.out.println(goal + "칸 남았습니다."); // 도착했을 때 break로 무한루프를 빠져나간다 if(goal

개발자가 되기까지 (2023.08.16~2024.04.15)/[Basic] Java2023. 8. 30. 17:11[Java] 주사위 던지기 (Math.random(), switch)

1~6까지의 수를 랜덤으로 추출하여 '주사위 : 1칸 전진' 출력 public class SwitchRandom { public static void main(String[] args) { // 1~6까지의 랜덤 생성 int dice = (int)(Math.random()*6)+1; //랜덤 수에 따른 출력 switch(dice) { case 1: System.out.println("주사위 : "+dice+"칸 전진"); break; case 2: System.out.println("주사위 : "+dice+"칸 전진"); break; case 3: System.out.println("주사위 : "+dice+"칸 전진"); break; case 4: System.out.println("주사위 : "+dic..

728x90
반응형
image