java随机数应用(填字小游戏)

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());
        }


    }




}

运行结果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值