这段java代码谁给我解释一下?

来源:百度知道 编辑:UC知道 时间:2024/05/30 23:40:06
import java.util.Random;

public class test2 {

public static void main(String[] args)
{

Random random1 = new Random();
int count = 0;
int[] cache =new int[7];
while(count<7)
{
int x = random1.nextInt(36);
if(isEqual(x,cache))
{
}
else
{
cache[count]=x;
count++;
System.out.println(x);
}
}
}
private static Boolean isEqual(int x,int[] cache)
{
for(int i=0;i<7;i++)
{
if(x == cache[i])
return true;
}
return false;
}
}

就是在每一句后面都加注释..
初学者..看不太懂啊..谢谢!!

//导入java.util.Random类
import java.util.Random;
//定义类test2
public class test2 {

//应用程序的主方法
public static void main(String[] args)
{
//定义一个Random对象
Random random1 = new Random();
//定义整形count变量,值为0
int count = 0;
//定义cache整形数组,元素个数为7
int[] cache =new int[7];
//当count<7时进入循环
while(count<7)
{
//定义一个随机数x
int x = random1.nextInt(36);
// 如果cache数组中存在x则什么也不做
if(isEqual(x,cache))
{
}
//否则
else
{
//将x的值赋给当前元素
cache[count]=x;
//将数字下标后移
count++;
//打印出x
System.out.println(x);
}
}
}
//定义isEqual静态函数,用来判断cache数组中是否存在x
private static Boolean isEqual(int x,int[] cache)
{
//当i<7时执行循环
for(int i=0;i<7;i++)
{
//如果x和当前元素相等,返回true
if(x == cache[i])
return true;
}
//如果没有元素和x相等,返回false
return false;
}
}

import java.util.Random;