搜索关键词...
您当前位置:主页 > 游戏大全 > 正文

JAVA猜字母游戏

2024-07-01 03:46 admin

JAVA猜字母游戏

import java.util.Random;

import java.util.Scanner;

public class Test {

private final static char[] ARRAY = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

private final static int NUM = 5;

private String s;

/**

 * 构造时生成ARRAY成员组成的随机字符串

 */

public Test() {

// 选择的字母数量

s = ;

// 随机数

Random r = new Random();

while (s.length() < NUM) {

// 如果新生成的字母不重复则保存

char tmp = ARRAY[r.nextInt(26)];

if (s.indexOf(tmp) < 0)

s += tmp;

}

}

public static void main(String args[]) {

Test test = new Test();

int time = 0;

loop: while (true) {

System.out.println(请输入5个不重复字母);

Scanner sc = new Scanner(System.in);

String line = sc.nextLine();

if (null == line || line.length() != 5) {

System.out.println(5个啊魂淡);

continue;

}

if (!line.matches(^[A-Za-z]+$)) {

System.out.println(字母啊魂淡);

}

line = line.toUpperCase();

char[] c = new char[5];

c = line.toCharArray();

int m = 0, n = 0;

for (int i = 0; i < NUM; i++) {

String strSub = line.substring(line.indexOf(c[i])+1);

if (!(strSub.indexOf(c[i]) < 0)) {

System.out.println(不重复啊魂淡);

continue loop;

}

int intTmp = test.s.indexOf(c[i]);

if(intTmp<0){

continue;

}

else{

m++;

if(intTmp==i)

n++;

}

}

if (n == NUM) {

time++;

System.out.println(完全正确!);

break;

} else {

time++;

System.out.println(输入正确 + m + 个字母.并且位置对了 + n + 个字母.);

continue;

}

}

System.out.println(共用 + time + 次);

}

}

字母游戏!!!

还剩21个,因为ET把UFO带走了!

精选应用 MORE +

好游安利换一换