KangMoo
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    ## 과제 : 숫자 야구 게임 목표 : 숫자 야구 게임을 구현현해본다. 요구사항 : 다음 조건을 만족하는 숫자 야구 게임을 구현한다. 1. **난수 생성**: 프로그램은 1부터 9까지의 서로 다른 숫자 3개를 임의로 생성한다. 이 숫자들은 게임 동안 변경되지 않으며, 플레이어가 맞춰야 할 대상이다. 2. **사용자 입력**: 플레이어는 1부터 9까지의 서로 다른 숫자 3개를 입력한다. 3. **판정 로직**: 컴퓨터는 플레이어가 입력한 숫자들과 초기에 생성한 난수를 비교하여 결과를 판정한다. 이때, 다음과 같은 용어를 사용한다 - **스트라이크**: 플레이어가 선택한 숫자가 위치와 숫자 모두 맞을 경우. - **볼**: 숫자는 맞지만 위치가 틀릴 경우. - **아웃**: 맞는 숫자가 하나도 없을 경우. 4. **결과 표시**: 게임은 플레이어의 입력마다 스트라이크, 볼, 아웃의 개수를 표시한다. 예를 들어, "2스트라이크 1볼" 또는 "3아웃"과 같이 표시한다. 5. **게임 종료 조건**: 플레이어가 3스트라이크를 달성하면 게임은 종료된다. 게임이 종료되면, 플레이어에게 승리 메시지와 함께 몇 번의 시도 끝에 성공했는지 알려준다. --- ## 과제 : 숫자 야구 게임 모범 답안 ```java import java.util.*; public class NumberBaseballGame { public static void main(String[] args) { // 난수 생성 int[] targetNumbers = generateRandomNumbers(); // 게임 루프 int attempts = 0; while (true) { // 사용자 입력 받기 System.out.println("남은 시도: " + (10 - attempts) + "번"); int[] userNumbers = getUserInput(); // 결과 체크 int strikes = countStrikes(targetNumbers, userNumbers); int balls = countBalls(targetNumbers, userNumbers); int outs = 3 - strikes - balls; // 결과 표시 System.out.println(strikes + " 스트라이크, " + balls + " 볼, " + outs + " 아웃"); // 게임 종료 조건 검사 if (strikes == 3) { System.out.println("축하합니다! " + attempts + "번의 시도 끝에 승리했습니다."); break; } attempts++; } } private static int[] generateRandomNumbers() { int[] numbers = new int[3]; Random random = new Random(); for (int i = 0; i < 3; i++) { while (true) { int randomNumber = random.nextInt(9) + 1; // 1부터 9까지의 난수 생성 boolean isUnique = true; for (int j = 0; j < i; j++) { // 중복 검사 if (numbers[j] == randomNumber) { isUnique = false; break; } } if (isUnique) { numbers[i] = randomNumber; break; } } } return numbers; } private static int[] getUserInput() { Scanner scanner = new Scanner(System.in); int[] numbers = new int[3]; System.out.print("1부터 9까지의 서로 다른 숫자 3개를 입력하세요: "); for (int i = 0; i < 3; i++) { while (true) { int number; try { number = scanner.nextInt(); } catch (Exception e) { System.out.println("잘못된 입력입니다. 숫자를 다시 입력해 주세요."); scanner.next(); // 잘못된 입력 스트림 비우기 continue; // 다시 입력 받음 } // 유효하지 않은 입력 처리 if (number < 1 || number > 9) { System.out.println("잘못된 숫자입니다. 1부터 9까지의 숫자를 입력해 주세요."); continue; // 다시 입력 받음 } // 중복 숫자 입력 처리 boolean isUnique = true; for (int j = 0; j < i; j++) { if (numbers[j] == number) { System.out.println("중복된 숫자입니다. 다른 숫자를 입력해 주세요."); isUnique = false; break; // 중복 발견 시 다시 입력 } } if (isUnique) { numbers[i] = number; break; // 유효한 숫자면 반복 중단 } } } return numbers; } private static int countStrikes(int[] targetNumbers, int[] userNumbers) { int strikes = 0; for (int i = 0; i < 3; i++) { if (targetNumbers[i] == userNumbers[i]) { strikes++; } } return strikes; } private static int countBalls(int[] targetNumbers, int[] userNumbers) { int balls = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (i != j && targetNumbers[i] == userNumbers[j]) { balls++; } } } return balls; } } ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully