package algorithm;


class WordScrambleEx1 {

public static void main(String[] args) {

String[] strArr = { "CHANGE", "LOVE", "HOPE", "VIEW" };


String answer = getAnswer(strArr);

String question = getScrambledWord(answer);


System.out.println("Question:" + question);

System.out.println("Answer:" + answer);

} // main


public static String getAnswer(String[] strArr) {

// 내용을 완성하세요.

int idx = (int) (Math.random() * strArr.length);

return strArr[idx];


}


public static String getScrambledWord(String str) {

// 내용을 완성하세요.

String temp = "";

String[] strCopy = new String[str.length()];

String scram = "";


for (int i = 0; i < str.length(); i++) {

strCopy[i] = "" + str.charAt(i);

}



for (int i = 0; i < 20; i++) { // 자리바꾸기20번 수행

int j = (int) (Math.random() * str.length());

int k = (int) (Math.random() * str.length());

// System.out.println("k:" + k + " j:" + j);

temp = strCopy[j];

strCopy[j] = strCopy[k];

strCopy[k] = temp;

}


for (int i = 0; i < strCopy.length; i++) {

// System.out.println(strCopy[i]);

scram += strCopy[i];

}


return scram;

} // scramble(String str)

}

'IT 잡다 > sorcecode' 카테고리의 다른 글

7장 연습문제  (0) 2015.04.07
7장 연습문제 19 re  (0) 2015.04.07
BaseballGame 소스코드  (0) 2015.04.03
BaseballGame02  (0) 2015.04.03
BaseballGame  (0) 2015.04.03
Posted by 파란개발자
,