代码效果:

java--补全诗句代码-LMLPHP

代码:

import java.util.Scanner;

/*
补全诗句
*/

public class game1 {

public static void main(String[] args){
int x=1,y=0;
while(x==1){
System.out.println("");
char d;
int m=5; //题目量
int cc[] = new int[m];
String Str[] = new String[m]; //题目
Str[0] = "轻轻的我走了,正如我轻轻的来.";
Str[1] = "我轻轻的招手,作别西天的云彩。";
Str[2] = "那河畔的金柳,是夕阳中的新娘。";
Str[3] = "波光里的艳影,在我的心头荡漾。";
Str[4] = "我挥一挥衣袖,不带走一片云彩。";
for(int n=0;n<m;n++)
cc[n]=n;
int i2 = (int)(Math.random()*cc.length); //出题的随机性
String str = Str[i2];
String s=new String(str);
int lens=s.length();
System.out.println("诗句内有"+lens+"个字符");
int aa[] = new int[lens];
for(int a=0;a<lens-1;a++)
aa[a]=a;
int i = (int) (Math.random() * aa.length); //获取随机数
System.out.println("随机数:"+i);
int n=i;
for(int b=0;b<lens;b++){ //输出题目
char c = str.charAt(b);
if(b==n){
System.out.print("_");
}
else
System.out.print(c);}
System.out.println();

char c2 = str.charAt(n);
Scanner sc = new Scanner(System.in);
System.out.print("请输入答案:");
char name = sc.next().charAt(0);
if(name==c2){
System.out.println("Ture"+" +1");
++y;
}
else{
System.out.println("False");
++x;
System.out.println("最终得分:"+y);
}

System.out.println("随机字:"+c2);
}

}
}

04-17 04:43