24.06.05

윤지현·2024년 6월 5일

TIL

목록 보기
6/75
post-thumbnail

Lv1. 랜덤 닉네임 생성

1. 사용자는 최소 27가지 이상의 닉네임 중 하나를 랜덤으로 출력 할 수 있습니다.

(아래의 키워드를 사용해주세요!)
- 기철초풍, 멋있는, 재미있는
- 도전적인, 노란색의, 바보같은
- 돌고래, 개발자, 오랑우탄

  • 실행 화면

- createRandomNickname 함수 생성하기 전

import java.util.Random;

public class RandomNicknameCreator {
    public static void main(String[] args) {
        String[] firstList = {"기철초풍", "멋있는", "재미있는"};
        String[] secondList = {"도전적인", "노란색의", "바보같은"};
        String[] thirdList = {"돌고래", "개발자", "오랑우탄"};

        Random random = new Random();

        int first_randomIndex = random.nextInt(firstList.length);
        int second_randomIndex = random.nextInt(secondList.length);
        int third_randomIndex = random.nextInt(thirdList.length);


        System.out.println("랜덤 닉네임 : " + firstList[first_randomIndex]+secondList[second_randomIndex] + thirdList[third_randomIndex]);
    }
}

- createRandomNickname 함수 생성

import java.util.Random;

public class RandomNicknameCreator {

    private String[] firstList = {"기철초풍", "멋있는", "재미있는"};
    private String[] secondList = {"도전적인", "노란색의", "바보같은"};
    private String[] thirdList = {"돌고래", "개발자", "오랑우탄"};

    private String createRandomNickname() {
        Random random = new Random();

        int first_randomIndex = random.nextInt(firstList.length);
        int second_randomIndex = random.nextInt(secondList.length);
        int third_randomIndex = random.nextInt(thirdList.length);

        String name = firstList[first_randomIndex]+secondList[second_randomIndex] + thirdList[third_randomIndex];
        return name;
    }

    public static void main(String[] args) {
        RandomNicknameCreator randomNicknameCreator = new RandomNicknameCreator();
        String myNickname = randomNicknameCreator.createRandomNickname();
        System.out.println("랜덤 닉네임 : " + myNickname);
    }
}

✔ 여기서 한 번 함수 사용 전/후의 실행시간을 비교해봤다.

  • 함수 사용 전

    - 함수 사용 후

  • 둘 다 시간이 11~16초 사이의 시간이 걸렸다.
  • System.currentTimeMillis()를 사용
profile
첫 시작

0개의 댓글