java猜数字游戏,JAVA猜数字游戏实验报告

发布时间:2023-01-09

本文目录一览:

  1. java游戏编程1A2B是一款十分经典的猜数字游戏,每局开始,计算机都会随机生成四个数字?
  2. java猜数字游戏?
  3. 用java编写一个猜数字游戏,
  4. java猜数字小游戏。用eclipse写的
  5. 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 <= max) {
            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 > 0 && 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);
        }
    }
}

java猜数字游戏?

import java.util.Random;
import java.util.Scanner;
/**
 * @Author: Cool_Wu
 * @Date: 2020-12-01 23:39
 */
public class GuessNumberGame {
    static int count = 0;
    static int answer = new Random().nextInt(100);
    public static void main(String[] args) throws Exception {
        System.out.println("猜数字游戏开始,该数字是一个0~100之间的整数");
        compareNum();
    }
    public static void compareNum() throws Exception {
        if (count >= 10){
            System.out.println("正确答案是:" + answer);
            System.out.println("你太笨了,下次再来吧!");
            return;
        }
        count++;
        int n = receiveNum();
        if (n < 0){
            throw new Exception("您输入的数字不符合要求,请重新输入!");
        }
        if (n > 99){
            throw new Exception("输入错误,请输入正确的数字!");
        }
        if (n < answer){
            System.out.println("太小了,再大一点!");
            compareNum();
        }
        if (n > answer){
            System.out.println("太大了,再小一点!");
            compareNum();
        }
        if (n == answer){
            System.out.println("恭喜你,猜对了!");
        }
    }
    public static int receiveNum() {
        System.out.println("请输入您猜的数字:");
        int n = new Scanner(System.in).nextInt();
        return n;
    }
}

运行结果

用java编写一个猜数字游戏,

package day06;
import java.util.Scanner;
//猜字符游戏
public class GuessingGame {
    //主方法
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int count = 0; //猜错的次数
        char[] chs = generate(); //随机生成的字符数组
        System.out.println(chs); //作弊
        while(true){ //自造死循环
            System.out.println("猜吧!");
            String str = scan.next().toUpperCase(); //获取用户输入的字符串
            if(str.equals("EXIT")){ //判断str是否是EXIT
                System.out.println("下次再来吧!");
                break;
            }
            char[] input = str.toCharArray(); //将字符串转换为字符数组
            int[] result = check(chs,input);  //对比
            if(result[0]==chs.length){ //位置对为5
                int score = chs.length*100 - count*10; //一个字符100分,错一次减10分
                System.out.println("恭喜你猜对了,得分:" + score);
                break; //猜对时跳出循环
            }else{ //没猜对
                count++; //猜错次数增1
                System.out.println("字符对:"+result[1]+"个,位置对:"+result[0]+"个");
            }
        }
    }
    //随机生成5个字符数组
    public static char[] generate(){
        char[] chs = new char[5];
        char[] letters = { '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'};
        boolean[] flags = new boolean[letters.length]; //1.
        for(int i=0;i<chs.length;i++){
            int index;
            do{
                index = (int)(Math.random()*letters.length); //0到25
            }while(flags[index]==true); //2.
            chs[i] = letters[index];
            flags[index] = true; //3.
        }
        return chs;
    }
    //对比随机数组与用户输入的数组
    public static int[] check(char[] chs,char[] input){
        int[] result = new int[2];
        for(int i=0;i<chs.length;i++){
            for(int j=0;j<input.length;j++){
                if(chs[i]==input[j]){ //字符对
                    result[1]++; //字符对个数增1
                    if(i==j){ //位置对
                        result[0]++; //位置对个数增1
                    }
                    break;
                }
            }
        }
        return result;
    }
}

java猜数字小游戏。用eclipse写的

import java.util.Scanner;
/**
 * Java命令行版 猜数字游戏
 * @author kaifang
 */
public class GuessNum {
    public static void main(String[] args) {
        System.out.println("======猜数字游戏======\n");
        int answer = (int)(Math.random() * 200 + 1);
        Scanner sr = new Scanner(System.in);
        while(true) {
            System.out.print("请输入你猜的数字(1-200):");
            int in = sr.nextInt();
            if (in > answer) {
                System.out.println("猜大了!\n");
            } else if(in < answer){
                System.out.println("猜小了!\n");
            } else {
                System.out.println("恭喜你,才猜对了!!!\n");
                break;
            }
        }
        sr.close();
    }
}

Java猜数字游戏

public static void main(String[] args) {
    // TODO 自动生成方法存根
    System.out.println("欢迎进入猜数字游戏!您只有10次机会!猜的数字在0到100之间");
    Random r = new Random();
    int num = r.nextInt(100);
    Scanner input = new Scanner(System.in);
    int cai;
    for (int i = 0; i < 10; i++) {
        System.out.print("输入竞猜数字:");
        cai = input.nextInt();
        if (cai < 0 || cai > 100) {
            System.out.println("数字在0到100之间");
            continue;
        }
        if (cai == num) {
            System.out.println("猜中数字,胜利了");
            break;
        } else {
            System.out.println("没有猜中");
        }
        if (i == 9) {
            System.out.println("时间到,竞猜失败");
        }
    }
}