package ch01;
import java.util.Random;
import java.util.Scanner;
/**
* @Author zxf
* @Description
* @Date 2023/3/17 10:26
*/
public class CaiChengYu {
public static void main(String[] args) {
int g = 0;
int q = 0;
int t = 1;
int p = 1;
String num4[] = new String[6];
Scanner input = new Scanner(System.in);
String[] arr = new String[]{"春暖花开", "十面埋伏", "千军万马", "白手起家",
"张灯结彩", "风和日丽", "万里长城", "人来人往", "自由自在", "助人为乐",
"狐假虎威", "春风化雨", "马到成功", "拔苗助长", "安居乐业", "走马观花",
"念念不忘", "落花流水", "张冠李戴", "一往无前"};
boolean n = true;
while (n){
for (; q < 5; q++) {
Random s = new Random();
int e = s.nextInt(20);
char[] chars = new char[4];
chars = arr[e].toCharArray();
Random random = new Random();
int a = random.nextInt(3); // 1
for (int i = 0; i < chars.length; i++) {
if (a == i) {
System.out.print("_");
} else {
System.out.print(chars[i]);
}
}
System.out.print("输入你的答案:");
char daan = input.next().charAt(0);
if (daan==chars[a]){
System.out.println("输入正确");
g++;
}else {
System.out.println("猜错了");
num4[q]=arr[e];
p++;
}
}
System.out.println("五题已经答完了."+"获得"+g+"分"+",是否继续:(y.是/n.否)");
for(int l= 0;l<= p-1;l++) {
if(num4[l]==null){
}else {
System.out.println("错的是:"+num4[l]);
}
}
char k = input.next().charAt(0);
if (k=='y'){
t++;
System.out.println("到达"+t+"关");
g=0;
q=0;
}else {
System.out.println("退出成功!!!");
n=false;
}
}
}
}
随机数提取数组的下标产生要猜测的成语,将成语的Sring转换成char类型。进行判断