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(); } }
猜数据游戏。。。