티스토리 뷰
반응형
Day 6 챌린지를 시작해보자.
강의 실습 코드부터 확인해보자.
import java.util.Random;
import java.util.Scanner;
public class MadLibs {
Scanner scanner = new Scanner(System.in);
String story;
String name;
String adjective1;
String adjective2;
String noun1;
String noun2;
String noun3;
String adverb;
String randomNums;
Random rand = new Random();
// Getters!
public String getStory() {
return story;
}
public String getName() {
return name;
}
public String getAdjective1() {
return adjective1;
}
public String getAdjective2() {
return adjective2;
}
public String getNoun1() {
return noun1;
}
public String getNoun2() {
return noun2;
}
public String getNoun3() {
return noun3;
}
public String getAdverb() {
return adverb;
}
public String getRandomNums() {
return randomNums;
}
// Setters
public void setStory(String newStory) {
this.story = newStory;
}
public void setName(String newName) {
this.name = newName;
}
public void setAdjective1(String newAdj1) {
this.adjective1 = newAdj1;
}
public void setAdjective2(String newAdj2) {
this.adjective2 = newAdj2;
}
public void setNoun1(String newNoun1) {
this.noun1 = newNoun1;
}
public void setNoun2(String newNoun2) {
this.noun2 = newNoun2;
}
public void setNoun3(String newNoun3) {
this.noun3 = newNoun3;
}
public void setAdverb(String newAdverb) {
this.adverb = newAdverb;
}
public void setRandomNums() {
int num = Math.abs(rand.nextInt()) % 100;
int index = 0;
int[] numberHolder = new int[3];
while (index < numberHolder.length) {
numberHolder[index] = num + index;
index++;
}
randomNums = "not " + numberHolder[0] + ", not " + numberHolder[1] + ", but " + numberHolder[2];
}
// Print Instructions to Player
public void printInstructions() {
System.out.println("Welcome to the MadLibs game. If you type in "
+ "words, we'll give you a story. Start by typing in a name.");
}
// Get data from player
public void enterName() {
setName(scanner.nextLine());
}
public void enterNoun1() {
System.out.println("Give me a noun");
setNoun1(scanner.nextLine());
}
public void enterNoun2() {
System.out.println("Give me another noun");
setNoun2(scanner.nextLine());
}
public void enterNoun3() {
System.out.println("Give me the last noun");
setNoun3(scanner.nextLine());
}
public void enterAdjective1() {
System.out.println("I need an adjective.");
setAdjective1(scanner.nextLine());
}
public void enterAdjective2() {
System.out.println("I really need alpaca... just kidding, "
+ "give me another adjective.");
setAdjective2(scanner.nextLine());
}
public void enterAdverb() {
System.out.println("PLEASE!! I really want an adverb!");
setAdverb(scanner.nextLine());
}
public void putTogetherTheStory() {
String story;
int num = Math.abs(rand.nextInt()) % 2;
if (num == 0) {
story = "Jesse and her best friend " + getName() + " went to Disney World today! "
+ " They saw a " + getNoun1() + " in a show at Magic Kingdom "
+ " and ate a " + getAdjective1() + " feast for dinner. The next day I"
+ " ran " + getAdverb() + " to meet Mickey Mouse in his " + getNoun2() + " "
+ " and than that night I gazed at the " + getRandomNums() + " " +
getAdjective2() + " fireworks shooting from the " + getNoun3() + ".";
} else {
story = "Amanda and her frenemy " + getName() + " went to the zoo last summer. " +
"They saw a huge " + getNoun1() + " and a tiny little " + getNoun2() + ". That night"
+ "they decided to climb " + getAdverb() + " into the " + getNoun3() + " to get a closer look. "
+ "The zoo was " + getAdjective1() + "at night, but they didn't care... "
+ "until " + getRandomNums() + " " + getAdjective2() + " apes yelled in their faces, making "
+ "Amanda and " + getName() + " sprint all the way back home.";
}
setStory(story);
}
public void play() {
enterName();
enterNoun1();
enterAdjective1();
enterAdjective2();
enterNoun2();
enterAdverb();
enterNoun3();
setRandomNums();
putTogetherTheStory();
System.out.println(getStory());
}
public static void main(String[] args) {
MadLibs game = new MadLibs();
game.printInstructions();
game.play();
}
}
MadLibs 클래스를 만들어
getters, setter를 이용해
Story를 만드는 클래스이다.
중간중간에 배열과 Iteration에 대한 설명도 나오는데
포스팅하려고 System.out.println 정확하게 따라치는게 너무 힘들었다.. ;;ㅋㅋ
아무튼 코드가 길지만
getter, setter 때문이라
이해하는데 별다른 어려움은 없을 것이다!!
궁금한 점이 있다면
댓글을 활용해주길 바라고
그럼 Day 6 코드 리뷰로 다음 포스팅에서 만나자!!
더 나은 내일을 위해 노력하자. 🔥
반응형
'해커랭크 챌린지' 카테고리의 다른 글
[hackerrank] hackerrank challenges Day 7 자바 강의 리뷰 (0) | 2022.06.30 |
---|---|
[hackerrank] hackerrank challenges Day 6 자바 코드 리뷰 (0) | 2022.06.29 |
[hackerrank] hackerrank challenges Day 5 자바 코드 리뷰 (0) | 2022.06.28 |
[hackerrank] hackerrank challenges Day 5 자바 강의 리뷰 (0) | 2022.06.28 |
[hackerrank] hackerrank challenges Day 4 자바 코드 리뷰 (0) | 2022.06.27 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 디버깅
- LinkedList
- 정보처리산업기사
- 풀이
- 소스코드
- 정보처리 산업기사
- ORM
- 자바
- challenges
- 해커랭크 챌린지
- 해커랭크 자바
- hackerrank challenges
- 백준
- hackerrank
- 강의
- 해커랭크
- 22 정보처리 산업기사
- 정보처리산업기사 공부법
- queue
- Java
- 챌린지
- 자바의 정석
- stack
- BAEKJOON
- 코드
- 그리디
- JPA
- 해커랭크 자바 챌린지
- 22 정보처리산업기사
- 개발자
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함