《java从入门到精通》-笔记

第二章 Eclipse
1,Cril+atl+/ 按键自动补全Java关键字
2,shift+alt+f 格式化

第三章 基本语法
1,程序开发 最好英文命名 首字母大写
2,static 静态变量
能以 类名.静态变量 在其他类里调用
局部变量与成员变量名字重复时,成员变量被屏蔽。
若需要使用,类名.静态变量
局部变量只在方法内有用
15/12/10 P82

3,&,|非短路运算符 两个表达式都会运算
4,位运算符
按位与& 都是1才是1 否则0
按位或| 都是0才是0 否则0
按位异或 同为则0 否则1
<<左移 = x2 >>右移 =/2
5,表达式?ture:false
6,变量互换 避免临时变量增加资源消耗
a=a^b;b=b^a;a=a^b;
异或运算符的使用
7,隐式类型转换规则 a+b=?

  • a:byte,short,char b:int -> int a:byte,short,char,int b:long ->long
    a:byte,short,char,int,long b:float ->float
    a:byte,short,char,int,long,float b:double->double 8,

强制类型转换
(类型名)要转换的值
9,注释占20%-50%为佳
练习题1:

public class Test{
    public static void main(String[] args){
        int a = 21<<4;
        System.out.println(a);
    }
}

2:

import java.util.Scanner;
public class Test{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        System.out.println("a=");
        int a = scan.nextInt();
        System.out.println("b=");
        int b = scan.nextInt();
        int c =(a == b?1:0);
        if(c==1)
            System.out.println("a=b");
        else
            System.out.println("max="+(a<b?b:a));           
    }
}

15/12/11 p97

3:

public class Test{
    public static void main(String[] args){
    float a = 38.9f;
    float b = 27.2f;
    System.out.println("矩形的面积为:"+a*b);
    }
}

第四章
1,case后不可实数 例如case 1.1 false
不可字符串 例如 case “OK”false
2,while(){}无分号
3,do{}while();有分号
4,foreach语句
for(元素变量x:遍历对象obj){
引用了x的java语句;
}
x不用初始化
5,a[index++]+ 索引自增

6,输出九九乘法表
public class Test{
    public static void main(String[] args){
        for(int i = 1; i < 10; i++){
            for(int j = 1; j < i;j++){
                System.out.print(j+"x"+i+"="+j*i+"\t");
                //不换行print
            }
            System.out.println();//输出空格
        }
    }
}

15/12/12 p113

1,break 退出当前循环
2,continue跳过其后的语句 进行下一次循环

**4.7实战练习**1,
import java.util.Scanner;
public class Test{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        System.out.println("输入一个整数:");
        int num = scan.nextInt();
        if(num%2==0)
            System.out.println("偶数");
        else
            System.out.println("奇数");
    }
}

15/12/13 p118

2

import java.util.Scanner;
public class Test {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("输入菱形的行数(奇数):");
        int line = scan.nextInt();
        diamond(line);
    }

    public static void diamond(int Line) {
        for (int i = 0; i < Line / 2 + 1; i++) {
            for (int j = Line / 2 + 1; j > i + 1; j--) {
                System.out.print(" ");
            }// 输出左上角的空白
            for (int j = 0; j < 2 * i + 1; j++) {
                if (j == 0 || j == 2 * i)
                    System.out.print("*");// 输出*
                else
                    System.out.print(" ");// 输出空白

            }
            System.out.println("");// 换行
        }

        /*
         * for (int i = 0; i < Line / 2; i++)// 行数 { for (int j = 0; j < i + 1;
         * j++) { System.out.print(" "); }// 输出右上角的空白 System.out.print("*");
         * 
         * System.out.print("*");
         * 
         * for(int j =Line-2;j>0;j--) { if(j==i+1||j==Line-2)
         * System.out.print("*"); else System.out.print(" "); }
         * 
         * System.out.println("");// 换行 }
         */
        for (int i = Line / 2 + 1; i < Line; i++) {
            for (int j = 0; j < i - Line / 2; j++) {
                System.out.print(" ");
            }

            for (int j = 0; j < 2 * Line - 1 - 2 * i; j++) {

                if (j == 0 || j == 2 * (Line - 1 - i)) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println("");
        }
    }
}

3,
public class Test {
    public static void main(String[] args) {
        int i = 1;
        double sum = 0;
        double num = 1;
        while (i <= 20) {
            num = i * num;
            sum = sum + 1 / num;
            i++;
        }
        System.out.println("1+1/2!+1/3!+...+1/20!=" + sum);
    }
}

第五章
1, int arr[]=new int[]{1,2,3};
2,数组元素类型 数组名字[][]
3,fill(int[]a,int value)//对数组中的元素进行替换

5.7

1,
public class Test {
    public static void main(String[] args) {
        int arr[]= new int[]{1,2,3,4,5,6,7,8};
        for(int i= 0;i<arr.length;i++)
        {
            System.out.print(arr[i]+"\t");
        }
    }
}
2,
public class Test {
    public static void main(String[] args) {
        int arr[] = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
        int max = arr[0];
        for (int x : arr) {
            if (max < x)
                max = x;
        }
        System.out.println(max);
    }
}
3,
public class Test {
    public static void main(String[] args) {
        int arr[][] = new int[][] { { 1, 1 }, { 2, 2 }, { 3, 3 } };
        for (int x[] : arr) {
            for (int e : x) {
                System.out.print(e + " ");
            }
            System.out.println();
        }
    }
}

第六章 字符串
1,String s=new String(“”);
2,太长不能分两行写
3,s.length()
//获取字符串长度
4,s.indexOf() s.lastIndexOf()
//索引第一次,最后一次出现位置 0是初始
5,str.charAt(in index)
//获取指定索引处的字符
6,str.trim()
//去除前导,尾部的空格
7,StringTokenizer(String str,String delim)
//delim 从str中去除的字符串
8,str.replaceAll(String regex,String replacement)
//替换
9,==比较的是内存位置 不用来比较字符串
10,boolean matches(String regex)
//匹配正则表达式

实战演练 6.8

1,
import java.util.Scanner;
import java.util.regex.Pattern;
public class Test {
    public static void main(String[] args){
        System.out.println("输入:");
        Scanner scan = new Scanner(System.in);
        String text = scan.nextLine();
        int num = 0;
        for(int i=0;i<text.length();i++)
        {   
            boolean matches = Pattern.matches("i{1}", ""+text.charAt(i));
            if(matches)  
                num++;
        }
        System.out.println(num);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值