JAVA实现猜成语⼩游戏(作业记录)
编写⼀个猜成语的⼩游戏。
要求:使⽤⼀个字符串数组来保存10个四字成语,例如:
String [] idioms = {“凉拌黄⽠”, “红烧肘⼦”, …};
使⽤⼀个字符数组来保存20个⽆关字符,例如:
String [] chars = {‘吃’, ‘喝’, ‘胡’, …};
每次游戏开始时,随机选择⼀个成语,然后提取其中的4个汉字,再从字符数组中随机选择另外6个汉字,组成⼀个10个汉字的数组(汉字顺序随机)。注意!如果成语中,有重复的汉字,则只算⼀个,那么要从字符数组中多选出汉字。然后提⽰⽤户成语中包含这10个汉字,让⽤户猜测成语,⽤户输⼊汉字的编号,程序反馈汉字在成语中的位置。⽤户只有6次猜错机会,当6次机会⽤完后,游戏结束(猜对不占⽤次数)。当⽤户完全猜测出成语后,程序输出⽤户猜错的次数。
package GuessIdioms;
import java.util.ArrayList;
javascript是运行在()的脚本语言
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author 芳芳
* @usefor learn
*/
java多线程设计模式pdfpublic class guessIdioms {
private static String[] idioms={"凉拌黄⽠","开开⼼⼼","努⼒学习","未来可期","不由⾃主","⼀事⽆成","百⽇做梦","先⼊为主","青⼭绿⽔","东⼭再起"};
private static String[] chars ={"吃","喝","胡","朱","龙","⽼","居","宇","化","放"};
private static String str ="[○○○○]";java手机游戏下载
private static Map<Character, Integer> idiomNumsMap =new HashMap<Character,Integer>();//统计成语字符类型个数
public static void main(String[] args){
int count =6;//剩余猜测次数
int num =0;
int tcount =0;//记录猜对次数
char[] t= CharArray();
List<Integer> listNum =new ArrayList<Integer>();
Map<Character, Integer> cMap =new HashMap<Character,Integer>();//统计猜对词的个数
Random random =new Random();
int index = Int(10);//产⽣[0,10)之间的随机整数
String idiom = idioms[index];
String s =randomSort(idiom);
System.out.println("猜测的成语:"+idiom);
System.out.println("四字成语中包含的汉字如下:");
for(int i=0;i<s.length();i++){
System.out.print(i+1+"."+s.charAt(i)+";");
}
System.out.println();
System.out.println(str);
@SuppressWarnings("resource")
Scanner in =new Scanner(System.in);
System.out.println("剩余猜测次数为"+count+"次,请输⼊你的猜测:");
while(true){
num= in.nextInt();
if(num<1||num >10){
count--;
if(count!=0){
System.out.println("要求输⼊数字在1~10,剩余猜测次数为"+count+"次,请再次输⼊你的猜测");
}else{
}else{
System.out.println("要求输⼊数字在1~10,并且你已经Game Over了");
break;
}
continue;
}
char ss = s.charAt(num-1);//选中的字符
if(idiom.indexOf(ss)!=-1&&((ss)==null ||(ss)< (ss))){
//猜中其中⼀个词
int index2 =0;
(ss)==null|| (ss)>1){
index2 = idiom.indexOf(ss);
t[index2+1]=ss;
}else{
index2=idiom.lastIndexOf(ss);
t[index2+1]=ss;
}
System.out.println(t);
System.out.println("恭喜你,"+"“"+ss+"”"+"字在成语中的位置是:"+(index2+1));
tcount++;
if(tcount!=4){
object toSystem.out.println("剩余猜测次数为"+count+"次,请再次输⼊你的猜测:");
}
(ss)!=null){//统计猜对词的个数
cMap.put(ss, (ss)+1);
}else{
cMap.put(ss,1);
}
}else if(idiom.indexOf(ss)==-1&&!ains(num)){
//没有猜中
count--;
if(count!=0){
System.out.println("对不起,“"+ss+"”字不在成语中,剩余猜测次数为"+count+"次"+",请再次输⼊你的猜测:"); }else{
System.out.println("对不起,“"+ss+"”字不在成语中,并且你已经Game Over了");
break;
}
}else{
//重复猜测
count--;
if(count==0){
System.out.println("对不起,“"+ss+"”字你已经猜过了,并且你已经Game Over了");
break;
}else{
System.out.println("对不起,“"+ss+"”字你已经猜过了,剩余猜测次数为"+count+"次"+",请再次输⼊你的猜测:"); }
}
if(tcount==4&& count!=0){
全局变量的作用是使得多个System.out.println("太棒了!你⼀共猜错了"+(6-count)+"次,已经猜出了整个成语!");
break;
}
listNum.add(num);
}
}
public static String randomSort(String idiom){
List<String> sList =new ArrayList<String >();
List<String> rList =new ArrayList<String>();
List<Integer> list =new ArrayList<Integer>();
new ArrayList<String>();
String string ="";
String string ="";
int randSeq =-1;
idiomNumsMap =idiomNum(idiom);
for(String str:chars){
sList.add(str);
}
for(int i=0;i<10-idiomNumsMap.size();i++){
Random random =new Random();
randSeq = Int(sList.size());//产⽣[0,10)之间的随机整数
rList.(randSeq));
}
for(@SuppressWarnings("rawtypes") Map.Entry Set()){ do{
Random random =new Random();
randSeq = Int(rList.size());//产⽣[0,10)之间的随机整数}ains(randSeq));
rList.add(randSeq, Key()+"");
list.add(randSeq);
}
for(int i=0;i<rList.size();i++){
html滚动字幕
string +=(i);
}
return string;
}
public static Map<Character, Integer>idiomNum(String str){
Map<Character, Integer> result =new HashMap<Character,Integer>();
for(int i=0;i<str.length();i++){
char ch = str.charAt(i);
(ch)!=null){
result.put(ch, (ch)+1);
}else{
result.put(ch,1);
}
}
return result;
}
}
运⾏结果如下‘
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论