古詩填字ゲーム第四講091121 B
先生はこれが網易の面接問題だと言った.
字遊びjava
出力:
白?山に依って尽くす
黄?海流に入る
欲?千里目
?1階に上がる
A.日、川、貧乏、さらに
B.貧乏、日、更、河
C.日、更に、貧乏、川
D.川、日、更、貧乏
答えを入力してください.
字遊びjava
import java.util.*;
public class {
public static void main(String[] args) {
String[] poems = " , , , ".split(",");
String rightWords = "";
String[] answers = new String[4];
int rightOption = (int)(Math.random()* 4);
//
for (int i = 0; i < 4; i++) {
rightWords += poems[i].charAt((int)(Math.random()* 5));
}
answers[rightOption] = rightWords;
//
for (int i = 0; i < answers.length; i++) {
if (i == rightOption) continue;
int[] pos = {0,1,2,3};
for (int j = 0; j < 10; j++) {
int x = (int)(Math.random()* 4);
int y = (int)(Math.random()* 4);
int tmp = pos[x];
pos[x] = pos[y];
pos[y] = tmp;
}
//
//----------------
String anAnswer = "" + rightWords.charAt(pos[0]) + rightWords.charAt(pos[1]) + rightWords.charAt(pos[2]) + rightWords.charAt(pos[3]);
if (anAnswer.equals(rightWords)) {
i--;
continue;
}
//
for (int j = 0; j < i; j++) {
if (anAnswer.equals(answers[j])) {
i--;
continue;
}
}
answers[i] = anAnswer;
}
//
for (int i = 0; i < poems.length; i++) {
System.out.println(poems[i].replace(rightWords.charAt(i), '?'));
}
//
for (int i = 0; i < answers.length; i++) {
System.out.print((char)('A' + i) + ". ");
System.out.print(answers[i].charAt(0) + ", ");
System.out.print(answers[i].charAt(1) + ", ");
System.out.print(answers[i].charAt(2) + ", ");
System.out.println(answers[i].charAt(3));
}
System.out.println(" :");
Scanner scanner = new Scanner(System.in);
String userInput = scanner.next();
int u = userInput.charAt(0) - 'A';
if (u==rightOption) {
System.out.println(" !");
} else {
System.out.println(" !");
System.out.println(" " + (char)('A' + rightOption));
}
}
}
出力:
白?山に依って尽くす
黄?海流に入る
欲?千里目
?1階に上がる
A.日、川、貧乏、さらに
B.貧乏、日、更、河
C.日、更に、貧乏、川
D.川、日、更、貧乏
答えを入力してください.