笔记

while循环语句
格式:
基本格式
while(判断条件语句){
    循环体语句;
}        
扩展格式
 初始化语句;
 while(判断条件语句){
             循环体语句;
    控制条件语句;
}
随机猜数字
package cn.llhc.day;

import java.util.Scanner;

/**
 * 随机产生    math.random()
 * math.random()*1000 ......0.0-999.9999999
 * (int) (Math.random()*1000)......0-999
 * (int) ((Math.ranfom()*1000)+1)......1-1000
 * 
 */
public class whileDemo {
    
    public static void main(String[] args){
        int num = (int) ((Math.random()*1000)+1);
        //System.out.println("要猜的数字"+num);
        //随机产生 math.random()
        //Math
        Scanner sc = new Scanner(System.in);
        int guess = sc.nextInt();
        while(guess != num){
            if(guess<num){
                System.out.println("猜小了!");
            }else {
                System.out.println("猜大了!");
            }
            System.out.println("继续猜");
            guess = sc.nextInt();
            
            }
        if (guess == num){
            System.out.println("猜对了");
            
        }
        
    }

}
do-while循环语句
格式
基本格式
do{
    循环体语句;
}
while(判断条件语句);

代码:
package cn.edtu.day;

import java.util.Scanner;

public class DowhileDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        Scanner sc = new Scanner(System.in);
        
        int num = (int) ((Math.random()*1000)+1);
        System.out.println("猜的数字"+num);
        System.out.println("请输入猜的数字");
        int guess = sc.nextInt();
        do{
            if(guess<num){
                System.out.println("猜小了!");
            }else if(guess>num){
                System.out.println("猜大了!");
            }
            System.out.println("继续猜");
            guess = sc.nextInt();
        }while(guess != num);
        if(guess == num){
            System.out.println("猜对了");
        }
    
    }

}


break和countinue区别:
countinue:使用循环结构中,用于结束本次循环继续下一次循环;
package cn.llhc.day;

public class forDemo2 {
    public static void main(String[] args){
        
        int i;
        
        for(i=1;i<15;i++){
            if(i%2==0){
                continue;
            }
            
            
                System.out.println(i);
            
                    
        }
    }

}
break:可以使用在switch结构和循环结构中,用于终止本次循环。
package cn.llhc.day;

public class forDemo2 {
    public static void main(String[] args){
        
        int i;
        
        for(i=1;i<15;i++){
            if(i%7==0){
                break;
            }
            
            
                System.out.println(i);
            
                    
        }
    }

}


数组
A:概念
数组是存储同一种数据类型的不同元素的容器。
B:数组的定义格式
格式一:数组类型【】数组名
格式二:数据类型数组名【】
数组的初始化:
初始化:数组中的数据元素进行分配空间,并且为每一个数组元素进行赋值。

两种:
1、动态初始化:初始化的时候,只指定数组的长度,由系统为数组分配初始值。
int[] arr = new int[6];//0,0,0,0,0,0
    arr[0] = 1;//1,0,0,0,0,0
2、静态初始化:初始化的时候,指定了每一个数组元素的初始化的值,由系统决定数组的长度。
int[] arr1 = {1,3,5};
    int[] arr2 = new int[]{1,4,6};


int[] arr3;
    //arr3 = {1,2,3} //错误的
    arr3 = new int[]{1,3,5}; //正确的


Java中的内存分配:
栈:
存储的局部变量;
局部变量就是定义在方法中的变量;
使用完毕之后就会被释放,立即回收。

堆:
存储的是new出来的对象;实体;
每一个对象都是有地址值的;
每一个对象的数据都是有默认值的;
     byte,short,long---0
    float,double------0.0
    char---------------'\u0000'
    boolean----------false
    引用类型----------null
   使用完毕之后,会在垃圾回收空闲的时候,被回收。

方法区:
本地方法区: (和系统有关系)
寄存器:        (给cpu使用的)

数组应用:身份证案列
package cn.edtu.day;

import java.util.Scanner;

/**
 * 数组的应用:
 * 身份证案例
 * @author Administrator
 *
 */
public class carddemo {
    public static void main(String[] args) {
        //身份证的前17位数字
        int card[] = new int[17];
        //1-17位对应的系数
        int date[] = {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 sum = 0;
        for(int i = 0; i < card.length; i++){
            System.out.println("请输入你身份证前17位!");
            card[i]=sc.nextInt();
        }
        for(int i = 0; i < card.length; i++){
            sum+=card[i]*date[i];
        }
        System.out.println("最后一位:"+code[sum%11]);
        
        
    }

}


冒泡排序
package cn.edtu.day;
/**
 * 冒泡排序:
 * 1、声明一个数组,存放8个元素
 * @author Administrator
 *
 */
public class SortedDemo {
    public static void main(String[] args) {
        int[] arr = {25,6,2,4,15,9,24,1};
        bubbleSorted();
    }
    /**冒泡排序*/
    public static void bubbleSorted(){
        int[] arr = {25,6,2,4,15,9,24,1};
        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("排序后数组:");
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值