public class Test {
public static void main(String[] args) {
int guessTime = 8
boolean restart = true
while (restart) {
System.out.println("请选择下列选项!")
System.out.println("1 ----开始猜数")
System.out.println("2 ----游戏参数设置")
System.out.println("9 ----退出")
boolean flag = true
String choose = null
Scanner in = null
while (flag) {
in = new Scanner(System.in)
choose = in.next()
if (!choose.matches("[129]")) {
System.out.println("输入错误,请重新输入")
continue
} else {
break
}
}
if ("2".equals(choose)) {
boolean chooseLevel = false
System.out.println("请选择游戏难度!")
System.out.println("1 ----难(4次)")
System.out.println("2 ----一般(6次)")
System.out.println("3 ----容易(8次)")
while (!chooseLevel) {
String s = in.next()
if (s.matches("[123]")) {
switch (Integer.parseInt(s)) {
case 1:
guessTime = 4
chooseLevel = true
break
case 2:
guessTime = 6
chooseLevel = true
break
case 3:
guessTime = 8
chooseLevel = true
break
default:
System.out.println("你输入的选择不存在(请输入1、2、3)")
break
}
} else {
System.out.println("你输入的选择不存在(请输入1、2、3)")
}
}
continue
}
if ("9".equals(choose)) {
System.exit(0)
}
if ("1".equals(choose)) {
String number = String.valueOf(Math.round(Math.random() * 100) + 1)
System.out.println(number)
boolean getIt = false
int count = 0
while (!getIt &&++count <= guessTime) {
System.out.println("请输入你猜的值(1-100)")
String s = in.next()
if (s.matches("\\d+")) {
try {
if (Integer.parseInt(s) == Integer.parseInt(number)) {
getIt = true
break
} else if (Integer.parseInt(s) >Integer.parseInt(number)) {
System.out.println("你输入的数字大了!")
} else {
System.out.println("你输入的数字小了!")
}
} catch (NumberFormatException e) {
System.out.println("你输入的数据超过Integer的最大范围!")
continue
}
} else {
System.out.println("你输入的数据不合法!")
continue
}
}
if (getIt) {
System.out.println("恭喜你猜对了,你的战斗力是" + Math.round((1 - count * 1.0 / guessTime) * 100) + "%")
} else {
System.out.println("超过次数,尚需努力")
}
in.nextLine()
in.nextLine()
}
}
}
}
你好,看你的要求估计你们没有学集合,所以就给你写了个数组的:public class Words {
static public int mostFrequent(int[] list) {
int max = 0
int index = 0
for(int i=0i<list.lengthi++){
if(max <list[i]){
max = list[i]
index = i
}
}
return index
}
static public int onList(String word, String[] list) {
for(int i=0i<list.lengthi++){
if(word.equals(list[i])){
return i
}
}
return -1
}
public static void main(String[] args) {
String str = "my sister ha ha,She me love to get set Me my girl tree me" //测试的,这个你可以随意
String[] words = str.toLowerCase().replace('.',' ').replace(',',' ').replace(" ", " ").split(" ")
int len = words.length
//考虑到二维数组不方便所以这里使用两个数组,二者之间是一一对应的
int[] count = new int[len]
String[] ss = new String[len]
for (int i=0i<words.lengthi++) {
String word = words[i].trim()
int f = onList(word,words)
if(f == -1){
ss[i] = word
count[i] = 1
}else{
ss[f] = word
count[f] += 1
}
}
int index = mostFrequent(count)
System.out.println("出现次数最多的单词是:" + ss[index] + "出现的次数为:" + count[index])
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)