大数据笔记5/8


今日学习了while 循环语句以及 do-while 循环语句炒作,
实现了一下 使用for查找水仙花数,以及使用while玩猜数游戏
在实现猜数游戏扩展了如何使用Math.random()产生随机数
以及java堆和栈,数组加异常处理和冒泡排序

while 循环语句(先判断后执行)

格式1

while(判断条件语句){

​ 循环体语句;

}

格式2

初始化语句;

while(判断条件语句){

​ 循环体语句;

​ 控制条件语句;

}

1557277974950

eg.

用 while 语句写猜数游戏

package cn.tedu.day03;

import java.util.Scanner;

/*
 * while循环:
 * 猜的数字:int num =456;
 * 条件:int guess;
 * while(){
 * 	猜数字;
 * 	guess < num;	猜小了
 * 	guess > num;	猜大了
 *  guess ==num;	猜对了,退出(break)。
 * }
 * 
 * */
public class WhileDemo {
	public static void main(String[] args){
		System.out.println("猜数游戏");
		System.out.println("");
		
		//其中Math.random()方法是一个可以产生[0.0,1.0)区间内的一个双精度浮点数的方法
        //Math.random()----------------------- 0.0-0.9999999....
		//产生一个100以内的整数
		int num = (int)(Math.random()*100);
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入数字 a : ");
		int a;
		while(true){
			a = sc.nextInt();
			if(a == num){
				System.out.println("恭喜你猜对了");
				break;
			}else if(a < num){
				System.out.println("你猜小了");
			}else{
				System.out.println("你猜大了");
			}
		}
	}
}
/*
	单精度浮点的表示范围:-3.40E+38 ~ +3.40E+38
	双精度浮点的表示范围:-1.79E+308 ~ +1.79E+308
*/

do-while 循环语句(先执行后判断)

格式

do{

​ 循环体语句;

}while(判断条件语句);

eg.

用 do-while 语句写猜数游戏

public static void main(String[] args){
		System.out.println("猜数游戏");
		System.out.println("");
		int num = (int)(Math.random()*100);
		System.out.println(num);
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入数字 a : ");
		int a;
		do{
			a = sc.nextInt();
			if(a == num){
				System.out.println("你猜对了");
			}else if(a < num){
				System.out.println("你猜小了");
			}else{
				System.out.println("你猜大了");
			}
		}while(a != num);
	}
break 和 continue 的区别

continue:使用循环结构中,用于结束本次循环继续下一次循环

break:可以使用在 switch 结构中和循环结构中,用于跳出当前结构

Random (随机数)

  • 创建对象

    Random r = new Random;

  • 获取随机数

    int num = r.nextInt(100);

//产生的数据在[0,100)之间,

JAVA中的内存分配

栈:

存储的是局部变量

局部变量就是定义在方法中的变量

使用完毕之后就会被释放,立即回收

堆:

存储的是new出来的对象:实体

  • 每一个对象都是有地址值的

  • 每一个对象的数据都是有默认值的:

    • byte short long—0

      float double----------0.0

      char------------------------’\u0000’

      boolean------------------false

      引用类型-----------------null

  • 使用完毕之后,会在垃圾回收起空闲的时候,被回收

  • 方法区:

  • 本地方法区:(和系统有关)

  • 寄存器:(给cpu使用的)

数组

A:数组概念
  1. 数组是存储同一种数据类型多个元素的容器。
  2. 数组既可以存储基本数据类型,也可以存储引用数据类型。
B:数组的定义格式
  • 格式1:数据类型[] 数组名;

  • 格式2:数据类型 数组名[];

注意:这两种定义做完了,数组中是没有元素值的。

C:数组的初始化

数组中的数组元素进行分配内存空间

  • 动态初始化:初始化的时候,只指定数组长度,由系统来为数组分配初始值
  • 静态初始化:初始化的时候,指定了每一个数组元素的初始化的值,由系统来决定数组长度

eg.

身份证验证

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);
		int num = 0;
		System.out.println("请输入18位身份证号");
		int i;
		for(i = 0; i < card.length; i++){
			card[i] = sc.nextInt();
		}
		//	System.out.println("请输入身份证的最后一位数字");
		char name = sc.next().charAt(0);
		for(i = 0; i < card.length; i++){
			num = num + card[i] * data[i];
		}
		System.out.println("身份证的最后一位应为:" + code[num % 11]);
		if(code[num % 11] == name){
			System.out.println("该身份证号正确");
		}else{
			System.out.println("该身份证号错误");
		}
	
	}
数组异常
  • 数组越界异常,访问了数组中不存在的索引

    java.lang.ArrayIndexOutOfBoundsException

  • 空指针异常,数组的引用没有指向对象,但是却在操作对象中的元素

    java.lang.NullPointerException

try-catch 异常处理

格式:

try{

​ 可能会发生异常的语句块;

}catch(Exception e){

​ 异常处理;

}

try{
    arr3 = null;
    System.out.println(arr3[1]);
}catch(NullPointerException er){
    System.out.println("发生了空指针异常");
}catch(ArrayIndexOutOfBoundsException e1){
    System.out.println("发生了越界异常");
}catch(Exception e){
    e.printStackTrace();
}finally{
	System.out.println("666");
}

冒泡排序

//升序排序
int[] arr = {12,3,4,31,6,9,1,2};
boolean isSorted;
for(int i = 0; i < arr.length; i++){
    isSorted = true;//假设剩下的元素已经排好序了
    for(int j = 0; j < arr.length - 1 - i; 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(arr[i]);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值