永发信息网

用java完成唐诗测试游戏:从每句随机去掉一个字_代替,然...

答案:1  悬赏:0  手机版
解决时间 2021-04-05 03:33
  • 提问者网友:浩歌待明月
  • 2021-04-04 11:52
用java完成唐诗测试游戏:从每句随机去掉一个字_代替,然...
最佳答案
  • 五星知识达人网友:罪歌
  • 2021-04-04 12:45
import java.util.Random;

public class Test {
public static void main(String[] args){
String tangshi = "锄禾日当午,汗滴禾下土,谁知盘中餐,粒粒皆辛苦。" ;
char[] c = tangshi.toCharArray() ;
Random ran = new Random() ;
int ran1 = ran.nextInt(4) ;
int ran2 = ran.nextInt(4)+6 ;
int ran3 = ran.nextInt(4)+12 ;
int ran4 = ran.nextInt(4)+18 ;

char[] xuanxiang = new char[4] ;
xuanxiang[0] = c[ran1] ;
c[ran1] = '_' ;
xuanxiang[1] = c[ran2] ;
c[ran2] = '_' ;
xuanxiang[2] = c[ran3] ;
c[ran3] = '_' ;
xuanxiang[3] = c[ran4] ;
c[ran4] = '_' ;

for(int i=0; i System.out.print(c[i]) ;
if((i==5)||(i==11)||(i==17)||(i==23)){
System.out.println() ;
}
}
int [] abcd = getABCD() ;
System.out.print("A、"+xuanxiang[abcd[0]]+" ");
System.out.print("B、"+xuanxiang[abcd[1]]+" ");
System.out.print("C、"+xuanxiang[abcd[2]]+" ");
System.out.print("D、"+xuanxiang[abcd[3]]);

}

public static int[] getABCD(){
int[] c = new int[4] ;
Random ran = new Random() ;
boolean flag = true ;

while(flag){
c[0] = ran.nextInt(4) ;
c[1] = ran.nextInt(4) ;
if(c[0] != c[1]){
c[2] = ran.nextInt(4) ;
if((c[0]!=c[2])(c[1]!=c[2])){
c[3] = ran.nextInt(4) ;
if((c[0]!=c[3])(c[1]!=c[3])(c[2]!=c[3])){
flag=false ;
}
}

}

}

return c;
}
}

如果是7字的诗你就自己修改吧!呵呵!
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯