import java.util.Random;
public class 随机数组 {
/*生成随机数
* [0,N): aa=new Random().nextInt(N)
* [1,N]: aa=new Random().nextInt(N)+1
*
* 终极公式
*int randNumber=rand.nextInt(Max-Min+1)+Min;
* 生成Min和Max范围内的随机数
*
*/
public static void main(String[] args) {
Random a = new Random();
// a.nextInt(101);
int[] arr=new int[5];
for (int i = 0; i < 5; i++) {
arr[i]=a.nextInt(100)+1;
System.out.print(arr[i]+",");
}
System.out.println("\r");
System.out.println( "length长度:"+ arr.length); //注意长度和索引值的区别
System.out.println("\n");
/* System.out.println(arr.length()); 带括号的是方法,不带括号的是属性 */
// System.out.println("随机数组"+arr[i]);
// System.out.println(arr[0]);
paixu(arr);
}
public static void paixu(int[] arr) {
for( int x=0;x<=arr.length-1;x++) {
for (int j = 0; j < arr.length-x-1; j++) { //相邻的比,只能找出最大值,外边的for才是吧最大值放右边
if(arr[j]>arr[j+1]) {
int tem=arr[j];
arr[j]=arr[j+1];
arr[j+1]=tem;
}
}
}
for (int k = 0; k < 5; k++) {
System.out.println("随机数组"+arr[k]);
}
}
}
package 补练;
import java.util.Random;
import java.util.Scanner;
public class 随机数组对比 {
public static void main(String[] args) {
int n,temp;
Scanner in = new Scanner(System.in);
System.out.print("输入想输出多少个随机数:");
n = in.nextInt();
int[] arr = new int[n];
Random rand = new Random();
System.out.println("输出"+n+"个随机数:");
for(int i=0;i<n; i++) {
arr[i] = rand.nextInt(100);
}
for(int i=0;i<n;i++) {
System.out.println(arr[i]);
}
System.out.println(" ");
//冒泡排序
System.out.println("冒泡排序从小到大输出:");
for(int j=0;j<arr.length-1;j++) {
//外层循环排序的趟数
for(int k=0;k<arr.length-j-1;k++) {
//内层循环一趟内的次数
if(arr[k]>arr[k+1]) {
temp = arr[k+1];
arr[k+1] = arr[k];
arr[k] = temp; //交换
}
}
}
for(int i=0;i<arr.length;i++) {
System.out.println(arr[i]);
}
}
}