使用Random类编写一个程序,把1-100中20个不重复的数放入数组中

package c003;
import java.util.Random;
public class Rondomlei {
	
	public static void main(String[] args) {
		Random random = new Random();
		
		int a[] = new int[20];
		int j=0;
		for(int i=0;i<a.length;i++) {
			int r=random.nextInt(100);
				a[i]=r;
				for(j=0;j<i;j++) {
					if(a[j]==a[i]) {
						i--;
						break;
				}
			}
		}
			
		for(int i=0;i<a.length;i++) {
			System.out.print(a[i]+" ");
		}
	}
	}



猜你喜欢

转载自blog.csdn.net/weixin_44517301/article/details/93333213