Java 學(xué)習(xí)第一周總結(jié)

計(jì)算機(jī)的一些基本常識(shí)

數(shù)據(jù)單位

在計(jì)算中表示數(shù)據(jù)的最小單位是比特 - bit - 一個(gè)0或者一個(gè)1

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

關(guān)于二進(jìn)制的拓展

// 123 = 1 * 10^2 + 2 * 10 ^ 1 + 3 * 10 ^ 0
// 一個(gè)字節(jié)
// 00000000
// 00000001
// 內(nèi)存中的-2
// 10000010 - 原碼
// 11111101 - 反碼
// 11111110 - 補(bǔ)碼
// 內(nèi)存中的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

變量和常量

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

基礎(chǔ)的程序

        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;           // 十進(jìn)制
        int a2 = 0101;          // 八進(jìn)制
        int a3 = 0x101;         // 十六進(jìn)制
        int a4 = 0b101;         // 二進(jìn)制  Java 7+
        long a5 = 12345678L;    // 長(zhǎng)整數(shù)
        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;       // 單倍精度浮點(diǎn)數(shù)
        double b2 = 1.23;       // 雙倍精度浮點(diǎn)數(shù)
        double b3 = 123.456;
        // 科學(xué)計(jì)數(shù)法
        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);
    }

}

練習(xí)

  1. 個(gè)人所得稅算法
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("請(qǐng)輸入你的五險(xiǎn)一金");
        double insurance = input.nextDouble();
        System.out.println("請(qǐng)輸入你的工資");
        double salary = input.nextDouble();
        double t = salary - 3500 - insurance;
        double x =0;
        double z = 0;
        
        if (t < 0) {
            System.out.println("你不需要繳納個(gè)人所得稅");
        } 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("你需要繳納的個(gè)人所得稅為:%.2f元\n" , tax);
        System.out.printf("本月實(shí)發(fā)工資為:%.2f元\n",(salary-tax-insurance));
        input.close();
    }
  1. 輸入x年x月,判斷這個(gè)月有多少天
import java.util.Scanner;
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("請(qǐng)輸入一個(gè)月份");
        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("這個(gè)月有" + 31 + "天");
                
                }
            else if (month == 4 || month == 6 || month == 9 || month == 11) {
                    System.out.println("這個(gè)月有" + 30 + "天");
            }
            if (month == 2) {
                System.out.println("請(qǐng)輸入一個(gè)年份");
                int year =input.nextInt();
                if ((year % 4 == 0 && year % 100 != 0) 
                    || year % 400 == 0) {
                    System.out.println("這個(gè)月有" + 29 + "天");
                }
                else {
                    System.out.println("這個(gè)月有" + 28 + "天");
                }
                
            }
        
        } else {
            System.out.println("這里需要一個(gè)整數(shù)??!");
        }
        // System.out.println("請(qǐng)輸入一個(gè)年份");

        input.close();
    }
  1. 三角形成立的條件和周長(zhǎng)面積
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("請(qǐng)輸入三條邊長(zhǎng)");
        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("三角形的周長(zhǎng)是:%.2f\n", p);
            System.out.printf("三角形的面積是:%.2f\n", S);
        } else {
            System.out.println("不能構(gòu)成三角形!");
        }
        input.close();
    }

4.分段函數(shù)

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("請(qǐng)輸入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.潤(rùn)年的判斷

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("請(qǐng)輸入一個(gè)年份");
        //分支結(jié)構(gòu)(選擇結(jié)構(gòu)) - 可以讓程序有多條執(zhí)行路徑
        if (input.hasNextInt()){
            int year = input.nextInt();
            boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) 
                    || year % 400 == 0;
            System.out.println(year + "年" + 
                    (isLeapYear ? "是" : "不是") + "潤(rùn)年");
            input.close();
        }
        else {
            System.out.println("瓜兮兮的,這里需要一個(gè)整數(shù)!!");
        }
        // 注釋:上題是判斷是否是潤(rùn)年。
    }

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

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        System.out.println("請(qǐng)按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("總計(jì)" + num + "點(diǎn)");
        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("現(xiàn)在平局,請(qǐng)按Enter繼續(xù)");
            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("總計(jì)" + newnum + "點(diǎn)");
            if (newnum == 7){
            System.out.println("莊家獲勝");
            break;
            }
            else if (newnum == num) {
                System.out.println("和第一輪總和相同,玩家獲勝");
                break;
            }
        }
            }
        input.close();
    }

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

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("請(qǐng)輸入捕魚的人數(shù)");
        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();
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,345評(píng)論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,494評(píng)論 3 416
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 176,283評(píng)論 0 374
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,953評(píng)論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,714評(píng)論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,186評(píng)論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,255評(píng)論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,410評(píng)論 0 288
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,940評(píng)論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,776評(píng)論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,976評(píng)論 1 369
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,518評(píng)論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,210評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,642評(píng)論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,878評(píng)論 1 286
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,654評(píng)論 3 391
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,958評(píng)論 2 373

推薦閱讀更多精彩內(nèi)容

  • 【程序1】 題目:古典問題:有一對(duì)兔子,從出生后第3個(gè)月起每個(gè)月都生一對(duì)兔子,小兔子長(zhǎng)到第三個(gè)月后每個(gè)月又生一對(duì)兔...
    葉總韓閱讀 5,146評(píng)論 0 41
  • Java經(jīng)典問題算法大全 /*【程序1】 題目:古典問題:有一對(duì)兔子,從出生后第3個(gè)月起每個(gè)月都生一對(duì)兔子,小兔子...
    趙宇_阿特奇閱讀 1,885評(píng)論 0 2
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,717評(píng)論 18 399
  • 運(yùn)算符 1.賦值運(yùn)算符 2.算數(shù)運(yùn)算符 +-*/ 3.關(guān)系運(yùn)算符 < <= > >= == != 4.邏輯運(yùn)算符 ...
    迷茫o閱讀 217評(píng)論 0 0
  • 托比林閱讀 549評(píng)論 0 50