大数据笔记3

一、循环控制语句二
  • (1)while循环语句
    • 格式:
      • 基本格式:
        while(判断条件语句){
        循环体语句;
        }
      • 扩展格式:
        初始化语句;
        while(判断条件语句){
        循环体语句;
        控制条件语句;
        }
  • 例句:利用while循环进行猜数字游戏:
    package com.until.day;
    /**
    * 思路:
    * while循环:
    * 猜的数字:int num = 456;
    * 条件:int guess;
    * while(guess == num){
    * 猜数字;
    * guess < num; 猜小了
    * guess > num; 猜大 了
    * guess == num; break;
    * }
    * 猜对了 ! 
    * */
    import java.util.Scanner;
    public class Until18 {
      public static void main(String[] args){
      Scanner sc = new Scanner(System.in);
      System.out.println("请输入你想要猜的数字:");
      int num = sc.nextInt();
      System.out.println("输入你要猜的值:");
      int guess = sc.nextInt();
      while(guess != num){
      	if(guess > num){
      		System.out.println("比数值要大,要往小猜!");
      		guess = sc.nextInt();
      	}
      	else{
      		System.out.println("比数值要小,要往大猜!");
      		guess = sc.nextInt();
      	}
      	if(guess == num){
      		System.out.println("猜对了!");
      		break;
      	}			
      }
      System.out.println("结果为:");
      System.out.println(guess);
     }
    }
    
  • (2)do while循环控制语句
    • 格式:
      • 基本格式:
        do{
        循环体语句;
        }while(判断条件语句)
  • 例句:利用do-while循环编写猜数字游戏
    package com.until.day;
    import java.util.Scanner;
    public class Until19 {
       public static void main(String[] args) {
          int num = (int) ((Math.random() * 1000)+1);
          System.out.println("要猜的数字:"+ num);
          Scanner sc = new Scanner(System.in);
          System.out.println("输入要猜的值:");
          int guess = sc.nextInt();
         do{
      	    if(guess > num){
      		  System.out.println("比数值要大,要往小猜!");
      		  guess = sc.nextInt();
          	}
      	    else{
      		  System.out.println("比数值要小,要往大猜!");
      		  guess = sc.nextInt();
      	    }
    
    
      	    if(guess== num){
      			  System.out.println("猜对了!");
      			  break;
      		    }			
      	    }while(guess != num);
             System.out.println("结果为:");
             System.out.println(guess);	
             sc.close();
           }
         }
    
  • (3)break和continue的区别
    • continue:使用循环结构,用于结束本次循环继续下一次循环。
      break:可以使用在switch结构和循环结构中,用于跳出当前结构。
  • 例句:利用continue和break进行编写
    package com.until.day;
    public class Until20 {
     public static void main(String[] args){
     	for(int i = 0;i <= 10;i++){
     		if(i== 3){
     			continue;//运行到3时,不会跳出循环,直接进行一个数的遍历,不输出3
     		}
     		System.out.println(i);
     	}
     	for(int j = 0;j <= 10;j++){
     		if(j == 3){
     			break; //运行到3 时会跳出循环,直接输出前面的数,不会输出3
     		}
     		System.out.println(j);
     	}		
      }
    }
    
二、数组

二、数组

  • A:数组概念
    数组是存储同一种数据类型多个元素的容器。
    数组既可以存储基本数据类型,也可以存储引用数据类型。

  • B:数组的定义格式
    格式1:数据类型[] 数组名;//普遍使用
    格式2:数据类型[] 数组名[];

  • C:数组的初始化
    格式:
    int[] arr = new int[4]//动态初始化0,0,0,0
    int[] arr = {1,2,3,4}//静态初始化
    int[] arr = new int[]{2,3,4,5};//静态初始化

    • 必须先初始化后使用,
      初始化:数组中的数组元素进行分配内存空间,并且为每一个数组元素进行赋值。
      两种:
      1、动态初始化:初始化时,只指定数组的长度,由系统来为数组分配初始值;
      2、静态初始化:初始化时,已经指定了每一个数组元素的初始化的值,由系统来决定数组的长度。
    • java中的内存分配:
      • 栈:
        存储的局部变量;
        局部变量就是定义在方法中的变量;
        使用完毕后会被释放,立即回收。
      • 堆:
        存储的是new出来的对象,实体;
        每一个对象都是由地址值得;
        每一个对象的数据都是有默认值的:
        byte , short,long —0
        float,double-----0.0
        char------’\u0000’
        boolean-----false
        引用类型-------null
        每一个对象使用完毕之后会在垃圾回收区空闲的时候,被回收。
      • 方法区:
        本地方法区(和系统有关系)
        寄存器(给CPU使用的)
  • 例句:身份证号的验证:
    package com.until.day;
    import java.util.Scanner;
    public class Until21 {
     public static void main(String[] args){
      //身份证的前17位数字
      int card[] = new int[17];
      //1-17位对应的数字
      int data[] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
      //身份证的最后一位系数
      char[] code = {'1','0','x','9','8','7','6','5','4','3','2'};
      Scanner sc = new Scanner(System.in);
      System.out.println("请输入前17位数字:");
      for(int i = 0;i < card.length; i++ ){
      	card[i] = sc.nextInt();		
      }
      int cj = 0;
      for(int i = 0; i< card.length;i++){			
      	cj += card[i] * data[i];
      }
      int j = cj % 11;
      System.out.println("最后一位为:" + code[j]);
     }
    }
    
  • D:数组的应用
    数组的访问:
    通过(数组名.length)来获取数组的长度(元素的个数)
    通过下标/索引来访问数组中的元素
    数组的遍历
    数组的复制
    数组的排序(冒泡排序)
  • 例句:对数组进行冒泡排序
    package com.until.day;
    public class Until23 {
    public static void main(String[] args){
      int arr[] = {16,18,5,56,12,32,8,46,7};
      bubbleSort(arr);				
    }
    public static void bubbleSort(int[] arr){//轮数	
      boolean isSorted;//假设剩下的元素已经排好序了		
      for(int i = 0; i < arr.length;i++){
      	isSorted = true;
      	for(int j = 0;j <arr.length - 1;j++){
      		if(arr[j] > arr[j+1]){
      			int temp = arr[j];
      			arr[j] = arr[j+1];
      			arr[j+1] = temp;
      			isSorted = false;//一旦需要交换数组元素,那就说明剩下的元素没有排好序
      		}	
      	}
      	System.out.println("排序的趟数:" + i);
      	if(isSorted){
      		break;//如果没有发生交换,说明剩下的元素已经排好序
      	}
      }
      System.out.println("排序后的数组为:");
      for(int i = 0; i < arr.length;i++){
      	System.out.println(arr[i]);
      }		
     }	
    }
    
  • E:数组异常
    • a:数组越界异常 (访问了数组中不存在的索引)java.lang.ArrayIndexOutOfBoundsException
    • b:空指针异常(数组的引用没有指向对象,但是却在操作对象中的元素) java.lang.NullPointerException
  • 例句:
    package com.until.day;
    public class Until22 {
     public static void main(String[] args){
      int arr[] = {1,3,5};
      //System.out.println(arr[3]);
      //java.lang.NullPointerException		
      try {
      	System.out.println(arr[3]);
      }catch(ArrayIndexOutOfBoundsException e){
      	System.out.println("数组发生越界异常");
      	
      }catch (Exception e) {
      	// TODO: handle exception
      }		
      try{
        arr = null;
        System.out.println(arr[1]);
        System.out.println(arr[3]);
      }catch(NullPointerException e){
      	System.out.println("发生了空指针异常");
      }catch(ArrayIndexOutOfBoundsException e){
      	System.out.println("数组发生越界异常");			
      }catch(Exception e){
      	e.printStackTrace();
      }finally{
      	System.out.println("233");
       }		
     }
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值