java字符串游戏

在这里插入代码片

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("游戏结束");

}

}

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值