import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* User: xiaomi
* Date: 17-9-22
* Time: 下午1:32
* To change this template use File | Settings | File Templates.
*/
public class DemoTest {
public static String randomStr(){
String [] array=new String[]{"world","jquery","spring","strust2","hello","family"};
String arrTostr=array[(int)(Math.random()*array.length)]; //从数组array中随机取一个单词
int repindex=(int)(Math.random()*arrTostr.length())*2;
StringBuffer stringBuffer=new StringBuffer(arrTostr);
for(int j=1;j<(arrTostr.length()-1)*2;j=j+2){
stringBuffer.insert(j,",");
}
String str=String.valueOf(stringBuffer);
String repstr= str.replace(str.charAt(repindex),'_');//单词中随机位置的字母用_代替
return str+"%"+repstr;
}
public static String fillWords(String randstr){
String str=randstr.split("%")[0];
String s=randstr.split("%")[1];
System.out.println(s.replace(",",""));
System.out.println("请输入:");
Scanner scanner = new Scanner(System.in);
String userinput = scanner.nextLine();
StringBuffer sb = new StringBuffer(s.replace("。",""));
if("exit".equals(userinput)){System.exit(1);}
int num=0;
for(int i=0;i<str.length();i++){
if(!(str.split(",")[i].equals(s.split(",")[i]))){
num=i;
break;
}
}
if(!userinput.equals(str.split(",")[num])){
System.out.println("输入错误!");
fillWords(randstr); }
else{
System.out.println("太棒了!!!!!");
System.out.println(sb.toString().replace("_", str.split(",")[num]).replace(",", ""));
}
return userinput;
}
public static void main(String [] args){
System.out.println("填字游戏开始!");
while (1==1){
fillWords(randomStr());
}
}
/**
* Created with IntelliJ IDEA.
* User: xiaomi
* Date: 17-9-22
* Time: 下午1:32
* To change this template use File | Settings | File Templates.
*/
public class DemoTest {
public static String randomStr(){
String [] array=new String[]{"world","jquery","spring","strust2","hello","family"};
String arrTostr=array[(int)(Math.random()*array.length)]; //从数组array中随机取一个单词
int repindex=(int)(Math.random()*arrTostr.length())*2;
StringBuffer stringBuffer=new StringBuffer(arrTostr);
for(int j=1;j<(arrTostr.length()-1)*2;j=j+2){
stringBuffer.insert(j,",");
}
String str=String.valueOf(stringBuffer);
String repstr= str.replace(str.charAt(repindex),'_');//单词中随机位置的字母用_代替
return str+"%"+repstr;
}
public static String fillWords(String randstr){
String str=randstr.split("%")[0];
String s=randstr.split("%")[1];
System.out.println(s.replace(",",""));
System.out.println("请输入:");
Scanner scanner = new Scanner(System.in);
String userinput = scanner.nextLine();
StringBuffer sb = new StringBuffer(s.replace("。",""));
if("exit".equals(userinput)){System.exit(1);}
int num=0;
for(int i=0;i<str.length();i++){
if(!(str.split(",")[i].equals(s.split(",")[i]))){
num=i;
break;
}
}
if(!userinput.equals(str.split(",")[num])){
System.out.println("输入错误!");
fillWords(randstr); }
else{
System.out.println("太棒了!!!!!");
System.out.println(sb.toString().replace("_", str.split(",")[num]).replace(",", ""));
}
return userinput;
}
public static void main(String [] args){
System.out.println("填字游戏开始!");
while (1==1){
fillWords(randomStr());
}
}
}
运行结果