初始Java8(练习题)
学习内容:
1.双色球
1 package day01; 2 import java.util.Arrays; 3 import java.util.Random; 4 public class ball{ 5 public static void main(String[] args) { 6 // TODO 自动生成的方法存根 7 //存放1~33号数字 8 String[]pool= {"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33"}; 9 //存放1~33数字是否使用过 10 boolean[] used=new boolean[pool.length]; 11 //存放双色球 12 String[]balls=new String[6]; 13 Random random=new Random(); 14 int i; 15 int index=0; 16 while(true) { 17 //随机生成数组下标 18 i=random.nextInt(pool.length); 19 if(used[i]) { 20 continue;//返回,重新开始循环 21 } 22 balls[index++]=pool[i];//取出一个球 23 used[i]=true;//标志使用过了 24 if(index==balls.length) {//检查是否满了 25 break;//结束循环 26 } 27 } 28 Arrays.sort(balls); 29 //对数组进行扩容 30 String[]newBalls=new String[7]; 31 for(int m=0;m<balls.length;m++) { 32 newBalls[m]=balls[m]; 33 } 34 newBalls[newBalls.length-1]=pool[random.nextInt(16)]; 35 //打印随机生成的双色球 36 System.out.println("双色球:"+Arrays.toString(newBalls)); 37 } 38 }
2. 对以下数据使用冒泡排序、插入排序、选择排序,进行从小到大排序:【1,5,6,9,8,2,3】
冒泡排序:
1 package day01; 2 import java.util.Arrays; 3 public class BubbleSortDemo { 4 public static void main(String[] args) { 5 // TODO 自动生成的方法存根 6 int[] ary= {1,5,6,9,8,2,3}; 7 int i,j; 8 for(i=0;i<ary.length;i++) { 9 for(j=0;j<ary.length-1;j++) { 10 if(ary[j]>ary[j+1]) { 11 int t=ary[j]; 12 ary[j]=ary[j+1]; 13 ary[j+1]=t; 14 } 15 } 16 } 17 System.out.println(Arrays.toString(ary)); 18 } 19 }
选择排序:
1 package day01; 2 import java.util.Arrays; 3 public class SelectSortDemo { 4 public static void main(String[] args) { 5 // TODO 自动生成的方法存根 6 int[] ary= {1,5,6,9,8,2,3}; 7 int i,j; 8 for(i=0;i<ary.length-1;i++) { 9 for(j=i+1;j<ary.length;j++) { 10 if(ary[i]>ary[j]) { 11 int t=ary[j]; 12 ary[j]=ary[i]; 13 ary[i]=t; 14 } 15 } 16 } 17 System.out.println(Arrays.toString(ary)); 18 } 19 }
插入排序:
1 package day01; 2 import java.util.Arrays; 3 public class InsertionSortDemo { 4 public static void main(String[] args) { 5 // TODO 自动生成的方法存根 6 int[] ary= {1,5,6,9,8,2,3}; 7 int i,j,k; 8 for(i=1;i<ary.length;i++) { 9 k=ary[i]; 10 for(j=i-1;j>=0&&ary[j]>k;j--) { 11 ary[j+1]=ary[j]; 12 } 13 ary[j+1]=k; 14 } 15 System.out.println(Arrays.toString(ary)); 16 } 17 }
3.使用工具类随机生成5位数,并使用工具类对这5位数进行排序
1 package day01; 2 import java.util.Random; 3 import java.util.Arrays; 4 public class RandomDemo { 5 public static void main(String[] args) { 6 int [] r=new int[5]; 7 for (int i = 0; i < r.length;i++) { 8 Random random=new Random(); 9 int temp=random.nextInt(1000); 10 r[i]=temp; 11 } 12 System.out.println("排序前:"+Arrays.toString(r)); 13 Arrays.sort(r); 14 System.out.println("排序后:"+Arrays.toString(r)); 15 } 16 }
4.自定义一个方法,当用户输入两个随机数,判断最大值
1 package day01; 2 import java.util.Scanner; 3 public class CompareDemo { 4 public static void main(String[] args) { 5 Scanner in = new Scanner(System.in); 6 System.out.print("请输入第一个数:"); 7 int a=in.nextInt(); 8 System.out.print("请输入第二个数:"); 9 int b=in.nextInt(); 10 System.out.println(a>b?"最大值为:"+a:"最大值为:"+b); 11 in.close(); 12 } 13 }
遇到的问题:
1.不会定义一个随机数的数组,上网查询后得知
2.在输入两个数比较大小的问题中,最初我采用了 Scanner a = new Scanner (System . in) ; Scanner b = new Scanner (System . in) ;然后比较 a 和 b 的大小将较大值赋给max 的方式,结果发现出现错误,a和b不能直接比较大小。
明天要学习的内容:
4.1-4.2.3