
正文
猜数组游戏java代码 猜数组游戏java代码大全
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java游戏编程1A2B是一款十分经典的猜数字游戏,每局开始,计算机都会随机生成四个数字?
package com.test;
import java.util.Random;
import java.util.Scanner;
/**
* 我的测试类
*
* @author 刘逸晖
*
*/
public class MyTest {
/**
* 生成不同的正整数随机数,返回字符串数组
*
* @param count
* 需要生成随机数的数量
* @param max
* 随机数的最大值
* @param min
* 随机数的最小值
* @return 生成的随机数
*/
private static String[] generateRandomNumber(int count, int min, int max) {
if (count 0 min -1 max min) {
String[] numbers = new String[count];
Random random = new Random();
// 生成随机数。
for (int i = 0; i numbers.length; i++) {
numbers[i] = min + random.nextInt(max - min) + "";
}
// 检查是否存在重复的随机数。
int equalIndex = areEqual(numbers);
while (equalIndex != -1) {
numbers[equalIndex] = min + random.nextInt(max - min) + "";
equalIndex = areEqual(numbers);
}
return numbers;
} else {// 参数不合法。
return null;
}
}
/**
* 判断字符串数组中的元素是否存在相等的
*
* @param array
* 预判断的数组
* @return 如果数组中有相等的元素,返回其下标;如果数组中没有相等的元素,或数组为空返回-1
*/
private static int areEqual(String[] array) {
if (array != null array.length 0) {
// 将数组中的每一个成员与其之前的所有成员进行比较,判断是否有相等的。
for (int current = 0; current array.length; current++) {
// 将当前便利的数组成员与其之前的所有成员进行比较,判断是否有相等的。
for (int previous = 0; previous current; previous++) {
if (array[current].equals(array[previous])) {
return previous;
}
}
}
}
return -1;
}
/**
* 搜索字符串数组
*
* @param array
* 数组
* @param value
* 预搜索的值
* @return 如果数组中有成员的值与预搜索的值相等返回成员下标,否则返回-1
*/
private static int search(String[] array, String value) {
if (array != null array.length -1 value != null) {
for (int i = 0; i array.length; i++) {
if (array[i].equals(value)) {
return i;
}
}
}
return -1;
}
public static void main(String[] args) {
System.out.println("欢迎你来到1a2b,输入n退出,输入y重新开始");
System.out.println("系统会随机产生4个0到9之间不同的数字,请你来猜");
System.out.println("输出a不仅代表你猜中了,还代表你猜对它的位置了哦!\r\n输出b则代表你猜中了,但位置不对哦");
// 开始循环,一次循环代表一局游戏。一局结束后立刻开启下一局。
while (true) {
System.out.println("新的一局开始了!");
// 产生随机数。
String[] randomNumbers = generateRandomNumber(4, 0, 9);
Scanner scanner = new Scanner(System.in);
// 创建变量存放输入记录。
String[] records = new String[] { "", "", "", "" };
// 创建变量存放ab结果。
String result = "";
// 请用户输入4次值。为什么请用户输入4次?因为数组中有4个成员。
for (int i = 0; i randomNumbers.length; i++) {
// 获得输入的值。
String inputValue = scanner.nextLine();
// 判断是否需要退出。
if (inputValue.equals("n") || inputValue.equals("")) {
System.out.println("Goodbye");
return;
}
// 创建变量定义是否忽略本次输入。
boolean ignore = false;
// 判断是否需要重新开始。
if (inputValue.equals("y")) {
ignore = true;
i = randomNumbers.length;
}
// 判断是否重复输入。
for (String record : records) {
if (inputValue.equals(record)) {
ignore = true;
i--;
System.out.println("这个值你已经输入过了哦!\r\n在给你一次机会。");
continue;
}
}
if (ignore) {
continue;
}
// 对输入的值进行搜索。
int searchResult = search(randomNumbers, inputValue);
// 如果搜索到了相关的值。
if (searchResult -1) {
// 记录。
records[i] = inputValue;
// 不仅搜索到了输入的值,并且位置正确。
if (searchResult == i) {
result = result + "a";
System.out.println("a");
} else {// 搜索到了输入的值,但位置错误。
result = result + "b";
System.out.println("b");
}
} else {// 输入错误。
System.out.println("这里没有这个值哦!\r\n再给你一次机会!");
i--;
}
}
System.out.println(result);
}
}
}
相关问答
Q1: Java猜数字游戏,事先随机给出3个100以内的正确答案,然后再输入100以内的数字猜,这个用数组怎么做呢?
Random r = new Random();
int num[] = new int[3];
for(int i = 0;i3;i++){
int n = r.nextInt(100);
num[i] = n;
}
System.out.println("请输入数字:");
Scanner s = new Scanner(System.in);
int input = s.nextInt();
boolean res = false;
System.out.println("我猜的数字"+input);
for (int i = 0; i num.length; i++) {
int j = num[i];
if(j==input)
res = true;
}
if(res)
System.out.println("猜对了");
else
System.out.println("猜错了");
System.out.println("3个正确答案:");
for (int i = 0; i num.length; i++) {
int j = num[i];
System.out.print(j+" ");
}
运行结果
Q2: 求教Java达人:用java编写一个猜数字游戏
package com.zuxia.cg.guest;
import java.util.Random;
import java.util.Scanner;
/**
* 猜数游戏 系统自动生成4个0-9猜数组游戏java代码的不重复数 用户猜 数字和系统生成的数是一样且位置相同就在数那个位置输出a猜数组游戏java代码,数相同但位置不同猜数组游戏java代码,则在数那个位置输出b
* 其他数字不变
*
* @author student
*
*/
public class test {
/**
* 产生不重复的随机数
*
* @return 一个数组
*/
public int[] rand() {
int[] array = new int[4];
Random rd = new Random();
for (int i = 0; i 4; i++) {
array[i] = rd.nextInt(10);
for (int j = 0; j i; j++) {
if (array[j] == array[i]) {
i--;
break;
}
}
}
return array;
}
/**
* 对产生的随机数组和用户的输入进行比较
* 对于数组位置和数字都一样的用‘a’代替猜数组游戏java代码,对于数组位置不同的但数字一样的用‘b’代替猜数组游戏java代码,并统计‘a’,‘b’的数量
*
* @param array
* @return 一个字符串
*/
public String get(int[] array) {
for (int i = 0; i 4; i++) {
System.out.println(array[i]); //输出系统生成的随机数
}
int[] list = new int[4];
Scanner scan = new Scanner(System.in);
System.out.print("请输入:");
for (int i = 0; i 4; i++) {
list[i] = scan.nextInt();
}
for (int i = 0; i 4; i++) {
for (int j = 0; j 4; j++) {
if (array[i] == list[j]) {
if (i == j) {
list[j] = 'a';
} else
list[j] = 'b';
}
}
}
int m = 0;
int n = 0;
for (int i = 0; i 4; i++) {
if (list[i] == 'a') {
m++;
} else if (list[i] == 'b') {
n++;
}
}
n = m + n;
System.out.print("\n" + m + "A" + n + "B\n");
return m + "A" + n + "B";
}
public static void main(String[] args) {
test test = new test();
int[] array = test.rand();
if (test.get(array).equals("4A4B")) {
} else {
while (!test.get(array).equals("4A4B")) {
test.get(array);
}
}
}
}
关于猜数组游戏java代码和猜数组游戏java代码大全的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







