Java猜数字游戏

 

package cn.edu.dcxy;

import java.util.Random;
import java.util.Scanner;

public class GuessNumber {
    public static void main(String[] args) {
        int[] data = new int[5];
        Random r = new Random();
        for (int i = 0; i < data.length; i++) {
            data[i] = r.nextInt(20) + 1;
        }

        Scanner sc = new Scanner(System.in);
        OUT:
        while (true){
            System.out.println("请输入一个1-20之间的整数进行猜测:");

            int guessData = sc.nextInt();
            for (int i = 0; i < data.length; i++) {
                if(guessData == data[i]){
                    System.out.println("您已经猜中了该数据,运气不错了!您猜中的数据索引是:" + i);
                    break OUT;
                }
            }
            System.out.println("当前猜测的数据在数组中不存在,请重新猜测!");
        }
        for(int i = 0; i < data.length; i++) {
            System.out.print(data[i]+"\t");
        }
        System.out.println();
    }
}

  

 

猜数据游戏。。。

posted @ 2022-11-30 10:17  宝山方圆  阅读(13)  评论(0编辑  收藏  举报