JAVA_数组_猜数游戏_练习_封装_02_ArrayDaemon.java,Array练习,Random练习,Scanner练习

//猜数游戏
import java.util.Random;
import java.util.Scanner;
public class ArrayDaemon{
	public static void main(String[] args){
		Random r = new Random();
		int[] numbers =new int[10];
		int len = numbers.length;
		for(int i=0;i<len;i++){
			numbers[i] = r.nextInt(20);
			System.out.print(numbers[i]+"/");
		}
		
		
		System.out.println("  请输入一个小于20的整数:");
		Scanner in = new Scanner(System.in);
		int userNum = in.nextInt();
		System.out.println("你输入的数字是:"+userNum);
		
		boolean flag = false;
		
		for(int i=0;i<len;i++){
			if(userNum==numbers[i]){
				//System.out.println("正确");
				flag = true;
				break;
			}
		}
		
		System.out.println(flag);
		if(flag==true){
			System.out.println("正确");
		}else{
			System.out.println("错误");
		}
	}
}




//数组的3种写法
public class ArrayTest{
	public static void main(String[] args){
		//1.
		int[] x;
		x = new int[3];
		x[0] = 10;
		x[1] = 20;
		x[2] = 30;
		
		int len = x.length;
		System.out.println(len);
		
		//2.
		int[] xx = new int[3];
		
		//3.
		int[] xxx = new int[]{11,12,13};
		
		System.out.println(xxx[0]);
		System.out.println(xxx[1]);
		System.out.println(xxx[2]);
	}
}


你可能感兴趣的:(JAVA_数组_猜数游戏_练习_封装_02_ArrayDaemon.java,Array练习,Random练习,Scanner练习)