자바 질문 있습니다

자바 질문 있습니다

작성일 2024.05.13댓글 2건
    게시물 수정 , 삭제는 로그인 필요

profile_image 익명 작성일 -

import java.util.Arrays; import java.util.List; import java.util.function.Function; public class CrapsGame { final static int GAME_ROUND = 15; public static void main(String[] args) { GamePoint gamePoint = new GamePoint(); for (int round = 1; round <= GAME_ROUND; round++) { System.out.printf("%2d ", round); GameLogic gameLogic = new GameLogic(); GameStatus gameStatus = null; while (true) { if ((gameStatus = gameLogic.rollDice()) != null) { break; } } gamePoint.increase(gameStatus); String capitalizeTarget = gameStatus.toString(); String capitalize = capitalizeTarget.substring(0, 1).toUpperCase() + capitalizeTarget.substring(1).toLowerCase(); System.out.printf(" %s\n", capitalize); } System.out.println(gamePoint); } } enum GameStatus { WON, LOST } class Dice { static int roll() { int dice1 = getRandom(); int dice2 = getRandom(); int sum = dice1 + dice2; System.out.printf("%2d(%d+%d)", sum, dice1, dice2); return sum; } static int getRandom() { return (int) (Math.random() * 6) + 1; } } class GameLogic { final static List<Integer> CRAPING_OUT_LIST = Arrays.asList(2, 3, 12); final static List<Integer> NATURAL_LIST = Arrays.asList(7, 11); final static int LOST_NUMBER = 7; int rollCount = 0; int point = 0; GameStatus rollDice() { ++rollCount; int sum = Dice.roll(); Function<Integer, GameStatus> processor = rollCount == 1 ? this::first : this::notFirst; GameStatus gameStatus = processor.apply(sum); if (gameStatus == null && rollCount == 1) { System.out.print(" ----"); } return gameStatus; } GameStatus first(int sum) { if (NATURAL_LIST.contains(sum)) { return GameStatus.WON; } else if (CRAPING_OUT_LIST.contains(sum)) { return GameStatus.LOST; } else { point = sum; } return null; } GameStatus notFirst(int sum) { if (point == sum) { return GameStatus.WON; } else if (LOST_NUMBER == sum) { return GameStatus.LOST; } return null; } } class GamePoint { int won = 0; int lost = 0; void increase(GameStatus gameStatus) { if (gameStatus == null) { throw new NullPointerException(); } if (gameStatus == GameStatus.WON) { won++; } else if (gameStatus == GameStatus.LOST) { lost++; } } @Override public String toString() { return String.format("Count of %-4s Game is %d\n", "Won", won) + String.format("Count of %-4s Game is %d\n", "Lost", lost); } }

심심해서 짜보다가 답변이 다른게 먼저 올라왔는데 그냥 올립니다

profile_image 익명 작성일 -

public class Main { public static void main(String[] args) throws Exception { for (int i = 0; i <= 15; i++) { System.out.println(play()); } } static int getSumOfTwoDice(){ int d1 = (int)(Math.random() * 6)+1; int d2 = (int)(Math.random() * 6)+1; int sum = d1 + d2; System.out.print(sum + "(" + d1 + "+" + d2 + ") "); return sum; } static String play(){ int first = getSumOfTwoDice(); if (first == 7 || first == 11) { return "Win"; } else if (first == 2 || first == 3 || first == 12) { return "Lost"; } else { System.out.println("---- "); while (true) { int second = getSumOfTwoDice(); if (second == first) { return "Win"; } else if (second == 7) { return "Lost"; } } } } }

깔끔하진 않지만 대충 해봤습니다.

안녕하세요 자바 질문있습니다.

대학교 과제를 만들고 있는데요. keylistener의 keypressed를 이용해서 캐릭터를 움직이는 것을 구현했습니다. 그런데 방향키를 꾹 누르면 한 칸 움직였다가, 0.5초?...

자바스크립트 함수에 대해 질문있습니다.

안녕하세요 현재 자바스크립트를 공부중인 초보자입니다. 자바스크립트를 공부하다 함수에 대해 알게 되었는데 문법으로만 봤을땐 이해가 어느정도 되는것같은데 직접...

자바스크립트 공부에 대해 질문있습니다.

현재 자바스크립트를 공부중인 프론트엔드 개발자 취업준비생입니다. 아직 실력이 많이 없다보니 힘든점이 많은데 인강을 보면서 투두리스트를 따라 만들때는 어렵긴하지만...

자바 질문있습니다

부모클래스에 default 생성자가 없으면 상속이 안되나요? 아래는 부모클래스 의 필드와 매개변수있는 생성자입니다. 안녕하세요. 생성자는 상속이 되지 않습니다. 부모의...

자바 질문있습니다.

[code] public class exc1 { public static void main(String[] args)throws Exception{ int[] arr= new int[10]; int arr[0] =Integer.parseInt("100");// 뒤에 10이...

자바 질문있습니다.

@Controller public class FileUploader_controller { @GetMapping( "/upload") public String f_upload() { return "fileUpload"; } private static final String F_PATH = "C:/Users/user8241...