Java 學習第一周總結

計算機的一些基本常識

數據單位

在計算中表示數據的最小單位是比特 - bit - 一個0或者一個1

  1. 字節(Byte) = 8比特(bit)
  2. 千字節(KB) = 1024B
  3. 兆字節(MB) = 1024KB
  4. 吉字節(GB) = 1024MB
  5. 太字節(TB) = 1024GB
    // 100Mbps ---> 12.5MBps
    // 1km ---> 1000m ---> 10^3
    // 1KB ---> 1024B ---> 2^10

關于二進制的拓展

// 123 = 1 * 10^2 + 2 * 10 ^ 1 + 3 * 10 ^ 0
// 一個字節
// 00000000
// 00000001
// 內存中的-2
// 10000010 - 原碼
// 11111101 - 反碼
// 11111110 - 補碼
// 內存中的127
// 01111111 = 2^6 + 2^5 + 2^4 + 2^3 + 2^2 + 2^1 + 2^0 = 2^7 - 1
// 01111111 11111111 = 2^15 - 1
// 01111111 11111111 11111111 11111111 = 2^31 - 1
01111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 = 2^63 - 1

變量和常量

    // 關鍵字 - 在程序中有特殊含義的單詞 - keyword
    //  表示類型的關鍵字:
    //      - byte(1) / short(2) / int(4) / long(8) 
    //      - float(4) / double(8)
    //      - char(2)
    //      - boolean(1bit) true(真) / false(假)
    // 標識符 - 給變量、常量、方法、類、接口等起的名字就叫標識符 - identifier
    // 標識符命名規則 :
    //      1. 字母(Unicode字符)、數字、下劃線和$, 數字不能開頭
    //      2. 大小寫敏感(區分大小寫)
    //      3. 不能使用關鍵字作標識符
    //      4. 見名知意
    //      5.  駝峰命名法
    //          - 命名類、接口、枚舉、注解:每個單詞首字母大寫
    //          - 命名變量、方法:第一個單詞全小寫從第二個單詞開始每個單詞首字母大寫
    //          - 命名常量用全大寫,多個單詞用下劃線分隔
    // 運算符 - operator
    //      - 賦值運算符 =、+=、-=、*=、/=、%=、……
    //      - 算術運算符 +、-、*、/、%
    //      - 比較運算符 >、<、>=、<=、==、!= - 結果一定是布爾值
    //      - 邏輯運算符 &(邏輯與運算)、|(邏輯或運算)
    //      - 短路運算符 &&(短路與運算-而且)、||(短路或運算-或者)
    // 字面量(直接量) - literal
    //      - 整數字面量: 123 0x123 0123 0b101 10L
    //      - 小數字面量: 1.23F 1.23 1.23e2 1.23e-2
    //      - 字符字面量: 'a' 'A' '昊' '\n' '\'' '\123'
    //      - 布爾字面量: true false
    //      - 字符串字面量: "hello"
    //      - 引用字面量: null
    //      - 類型字面量: String.class int.class
    // 分隔符 - separator
    //      - ; , { } ( ) [ ] : 空格

基礎的程序

        System.out.println(int.class);
        System.out.println(String.class);
        
        int x =3, y = 5, z = 3;
        // System.out.println(x > y);       // false
        // System.out.println(x != y);      // true
        // System.out.println(x == y);      // false
        // System.out.println(x >= z);      // true
        
        System.out.println(x > y && y > z);     // false
        System.out.println(x > y || y > z);     // true
        
        // final int CAPACITY_OF_ROOM = 120;
        // int ageOfStudent;

        // double A = 100.1;
        int a = 10;
        a += 20;    // a = a + 20;
        a *= 5;     // a = a * 5;
        a *= a + 2; // a = a * (a + 2);
        System.out.println(a);
        // int 1a = 1000;
        // int public = 12;
        // double static = 3.5;
        
        int a1 = 101;           // 十進制
        int a2 = 0101;          // 八進制
        int a3 = 0x101;         // 十六進制
        int a4 = 0b101;         // 二進制  Java 7+
        long a5 = 12345678L;    // 長整數
        int a6 = 12_345_678;    // Java 7+
        System.out.println(a1);
        System.out.println(a2);
        System.out.println(a3);
        System.out.println(a4);
        System.out.println(a5);
        System.out.println(a6);
        
        float b1 = 1.23F;       // 單倍精度浮點數
        double b2 = 1.23;       // 雙倍精度浮點數
        double b3 = 123.456;
        // 科學計數法
        double b4 = 1.23456e2;
        double b5 = 1.23456e-2;
        System.out.println(b1);
        System.out.println(b2);
        System.out.println(b3);
        System.out.println(b4);
        System.out.println(b5);
        
        System.out.println(Byte.MIN_VALUE + "~" + Byte.MAX_VALUE);
        System.out.println(Short.MIN_VALUE + "~" + Short.MAX_VALUE);
        System.out.println(Integer.MIN_VALUE + "~" + Integer.MAX_VALUE);
        System.out.println(Long.MIN_VALUE + "~" + Long.MAX_VALUE);
        System.out.println(Float.MIN_VALUE + "~" + Float.MAX_VALUE);
        System.out.println(Double.MIN_VALUE + "~" + Double.MAX_VALUE);
        
        char $x = 'A';
        char $y = '昊';
        // String name = "王大錘";
        System.out.println($x);
        System.out.println((int) $x);
        System.out.println($y);
        System.out.println((int) $y);
        System.out.println((char) 26115);
        
        boolean z0 = true;
        boolean z1 = false;
        System.out.println(z0);
        System.out.println(z1);
    }

}

練習

  1. 個人所得稅算法
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("請輸入你的五險一金");
        double insurance = input.nextDouble();
        System.out.println("請輸入你的工資");
        double salary = input.nextDouble();
        double t = salary - 3500 - insurance;
        double x =0;
        double z = 0;
        
        if (t < 0) {
            System.out.println("你不需要繳納個人所得稅");
        } else if (t <= 1500) {
            z = 0.03;
            x = 0;
        } else if (t <= 4500) {
            z = 0.1;
            x = 105;
        } else if (t <= 9000) {
            z = 0.2;
            x = 555;
        } else if (t <= 35000) {
            z = 0.25;
            x = 1005;
        } else if (t <= 55000) {
            z = 0.3;
            x = 2755;
        } else if (t <= 80000) {
            z = 0.35;
            x = 5505;
        } else {
            z = 0.45;
            x = 13505;
        }
        double tax = t * z - x;
        System.out.printf("你需要繳納的個人所得稅為:%.2f元\n" , tax);
        System.out.printf("本月實發工資為:%.2f元\n",(salary-tax-insurance));
        input.close();
    }
  1. 輸入x年x月,判斷這個月有多少天
import java.util.Scanner;
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("請輸入一個月份");
        if (input.hasNextInt()) {
            int month = input.nextInt();
            if (month == 1 || month == 3 || month == 5 || month == 7 || 
                    month == 8 || month == 10 || month == 12) {
                System.out.println("這個月有" + 31 + "天");
                
                }
            else if (month == 4 || month == 6 || month == 9 || month == 11) {
                    System.out.println("這個月有" + 30 + "天");
            }
            if (month == 2) {
                System.out.println("請輸入一個年份");
                int year =input.nextInt();
                if ((year % 4 == 0 && year % 100 != 0) 
                    || year % 400 == 0) {
                    System.out.println("這個月有" + 29 + "天");
                }
                else {
                    System.out.println("這個月有" + 28 + "天");
                }
                
            }
        
        } else {
            System.out.println("這里需要一個整數!!");
        }
        // System.out.println("請輸入一個年份");

        input.close();
    }
  1. 三角形成立的條件和周長面積
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("請輸入三條邊長");
        double a = input.nextDouble();
        double b = input.nextDouble();
        double c = input.nextDouble();
        boolean isvalid = (a + b) > c && (a + c) > b && (b + c) > a;
        if (a > 0 && b > 0 && c > 0 && isvalid) {
            double p = a + b + c;
            double S = Math.sqrt(p / 2 * (p / 2 - a) * (p / 2 - b) * (p / 2 - c));
            System.out.printf("三角形的周長是:%.2f\n", p);
            System.out.printf("三角形的面積是:%.2f\n", S);
        } else {
            System.out.println("不能構成三角形!");
        }
        input.close();
    }

4.分段函數

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("請輸入x=");
        double x = input.nextDouble();
        double y;
        if (x < -1) {
            y = 3 * x + 5;
        } else if (x <= 1) {
            y = x - 1;
        } else {
            y = 5 * x - 3;

        }
        System.out.println("y =" + y);
        input.close();
    }

5.潤年的判斷

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
        // int a = input.nextInt();
        // int b = input.nextInt();
        // System.out.println(a>b?a:b);
        System.out.println("請輸入一個年份");
        //分支結構(選擇結構) - 可以讓程序有多條執行路徑
        if (input.hasNextInt()){
            int year = input.nextInt();
            boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) 
                    || year % 400 == 0;
            System.out.println(year + "年" + 
                    (isLeapYear ? "是" : "不是") + "潤年");
            input.close();
        }
        else {
            System.out.println("瓜兮兮的,這里需要一個整數!!");
        }
        // 注釋:上題是判斷是否是潤年。
    }

6.Graps賭博游戲
游戲規則:
//-兩顆篩子
//-第一次:
//玩家搖出 7或 11 玩家勝
// 2 3 或 12 莊家勝
// 其他點數 游戲繼續
//玩家再搖
// 如果搖出了7點 莊家勝
// 如果搖出了第一次的點數 玩家勝
// 其他情況 游戲繼續

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        System.out.println("請按Enter開始游戲");
        String x = input.nextLine();
        int a =(int) (Math.random()*6+1);
        int b =(int) (Math.random()*6+1);
        int num = a + b;
        int n = 1;
        System.out.println("第1次你搖出了" + a + "和" + b );
        System.out.println("總計" + num + "點");
        if (num == 7 || num == 11){
            System.out.println("第1局玩家獲勝");
        }
        else if (num == 2 || num == 3 || num == 12) {
            System.out.println("第1局莊家獲勝");
        }
        else {
            while(true){
            System.out.println("現在平局,請按Enter繼續");
            String y = input.nextLine();
            int c =(int) (Math.random()*6+1);
            int d =(int) (Math.random()*6+1);
            int newnum = c + d;
            n+=1;
            System.out.println("第" + n + "次你搖出了" + c + "和" + d );
            System.out.println("總計" + newnum + "點");
            if (newnum == 7){
            System.out.println("莊家獲勝");
            break;
            }
            else if (newnum == num) {
                System.out.println("和第一輪總和相同,玩家獲勝");
                break;
            }
        }
            }
        input.close();
    }

7.捕魚問題:
n個人,第1個人先將魚扔掉一只然后平分五份拿走其中一份,第2個人也將魚扔掉一只然后平分五份拿走其中一份,以此類推,直到第n個人的時候也扔掉一只然后平分五份拿走其中一份,求這堆魚至少有多少只。

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("請輸入捕魚的人數");
        int n = input.nextInt();
        
        
        for (int a = 1; a <= Integer.MAX_VALUE; a++) {
            int i = 1;
            int b = a;
            boolean x = true;
            while (i <= n && x) {
                if (b % n == 1) {
                    b = (n - 1) * (b - 1) / n;
                    i++;
                    x = true;
                } else {
                    i++;
                    x = false;
                }
            }
            if (x && i==(n+1)) {
                System.out.print(a);
                break;
            }
        }
        input.close();
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 【程序1】 題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一對兔...
    葉總韓閱讀 5,166評論 0 41
  • Java經典問題算法大全 /*【程序1】 題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子...
    趙宇_阿特奇閱讀 1,916評論 0 2
  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,767評論 18 399
  • 運算符 1.賦值運算符 2.算數運算符 +-*/ 3.關系運算符 < <= > >= == != 4.邏輯運算符 ...
    迷茫o閱讀 222評論 0 0
  • 托比林閱讀 552評論 0 50