在这里插入代码片
package com.game;
import java.util.Random;
import java.util.Scanner;
public class Game {
Scanner input = new Scanner(System.in);
String zu[] = new String[20];
int level[][] = { { 1, 2, 3, 5, 3 }, { 4, 5, 6, 3, 7 }, { 3, 5, 7, 9, 6, }, { 5, 8, 9, 7, 8 }, { 9, 8, 10, 11, 13 },
{ 10, 11, 12, 13, 9 } };
public void menu() {
System.out.println("是否开始游戏(y/n)");
String a = input.next();
if (a.equals("y")) {
ran();
} else if (a.equals("n")) {
System.out.println("退出游戏");
} else {
System.out.println("请输入正确格式");
menu();
}
}
StringBuffer buffer = new StringBuffer();
Random random = new Random();
// 通过循环生成要输出的字符串
public void ran() {
int s=0;
int c = 1;
int j = 0;
int i;
int x=10;
ok: do {
do {
int len = 5;
int arrIdx = random.nextInt(len - 1);
int num = level[s][arrIdx];
for (i = 0; i < num; i++) {
int rand = (int) (Math.random() * 5); // 产生随机数
// 根据随机数拼接字符串
switch (rand) {
case 0:
buffer.append(">");
break;
case 1:
buffer.append("<");
break;
case 2:
buffer.append("g");
break;
case 3:
buffer.append("y");
break;
case 4:
buffer.append("z");
break;
case 5:
buffer.append("s");
break;
}
}
System.out.println(buffer);
long currentTime1 = System.currentTimeMillis();
String l1 = new String(buffer.toString());
String inner = input.next();
long currentTime2 = System.currentTimeMillis();
// String out = String(buffer);
if (currentTime2 - currentTime1 > 20000) {
System.out.println("您已超时");
return;
} else {
if (l1.equals(inner)) {
j = j + num;
if(j>x) {
s++;
}
System.out.println(
"输入正确,您的积分为" + j + ",您的级别为" + s + "已用时间为" + (currentTime2 - currentTime1) / 1000 + "秒");
buffer.delete(0, buffer.length());
} else {
break ok;
}
}
} while (j < x);
j=j-x;
x=x+5;
} while (s < 5);
if(s==5)
System.out.println("您已通关");
}
}
package com.game;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Game m=new Game();
m.menu();
System.out.println("游戏结束");
}
}