JerryWang168
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 【韩顺平讲Java】Chapter4 - 運算符 *** > **即使再小的帆也能遠航** > 參考自[【零基础 快速学Java】韩顺平 零基础30天学会Java](https://www.bilibili.com/video/BV1fh411y7R8/?vd_source=c5074574112ef27dae243d70aa2175b8) >###### tags: `韓順平講Java` *** # 運算符介紹 ![](https://i.imgur.com/gK9Ux0c.png =600x) # 算數運算符 ![](https://i.imgur.com/FmoTh4r.png) ```java= public class ArithmeticOperator{ public static void main (String[] args){ System.out.println(10 / 4); // 2 (因為是int運算) System.out.println(10.0 / 4); //2.5 double d = 10 / 4; // java 中 10/4 =2, 2 => 2.0 System.out.println(d); // 2.0 // 取模% =============== // 在%的本質,看一個公式 a % b = a - (int)(a/b)*b System.out.println(10 % 3); // 1 // -10 % 3 = -10 - (int)((-10) / 3) * 3 = -1 System.out.println(-10 % 3); // -1 System.out.println(10 % -3); // 1 System.out.println(-10 % -3); // -1 } } ``` ```java= int i1 = 10; int i2 = 20; int i = i1++; // i = 10, i1 = 11 System.out.println("i="+i); //10 System.out.println("i2="+i2); //20 i = --i2; // i2 = 19, i = 19 System.out.println("i="+i); //19 System.out.println("i2="+i2); //19 ``` ## 課堂練習1 ```java= int i1 = 10; int i2 = 20; int i = i1++; // i = 10, i1 = 11 System.out.println("i="+i); //10 System.out.println("i2="+i2); //20 i = --i2; // i2 = 19, i = 19 System.out.println("i="+i); //19 System.out.println("i2="+i2); //19 ``` ## 課堂練習2 1. 假如還有59天放假,問合XX個星期零XX天 ```java= public class ArithmeticOperatorExercise02{ public static void main (String[] args){ int days = 59; System.out.println("59天 = "+ (days/7)+"個星期又"+(days%7)+"天"); } } ``` 2. 定義一個變量保存華氏溫度,華氏溫度轉換攝氏溫度的公式為: C = ==5/9==*(F-100),求華氏溫度對應攝氏溫度(234.5) ```java= public class ArithmeticOperatorExercise02{ public static void main (String[] args){ double dF = 234.5; // 5/9在java運算中會變0,要修正 double dC = 5.0/9*(dF-100); System.out.println(dF+"華氏溫度 = "+ dC+"攝氏溫度"); } } ``` ## 面試題 ```java= // 1. int i = 1; i = i++; // 規則 : 使用臨時變量 (1) temp=i;(2) i=i+1;(3) i=temp; System.out.println(i); // 1 // 2. int i = 1; i = ++i; // 規則 : 使用臨時變量 (1) i=i+1;(2) temp=i;(3) i=temp; System.out.println(i); // 2 ``` # 關係運算符(比較運算符) ![](https://i.imgur.com/G05VbFm.png) # 邏輯運算符 - `&` / `|` 在運算時,後面的判斷都會執行,效率低 ![](https://i.imgur.com/sYUAx78.png) ![](https://i.imgur.com/XmSVIR3.png) ![](https://i.imgur.com/DT5fVSS.png) ## 練習 ```java= int x = 5;int y = 5; if (x++==6 & ++y==6){ x=11; } System.out.println("x="+x+",y="+y); // 6,6 ``` ```java= int x = 5;int y = 5; if (x++==6 && ++y==6){ x=11; } System.out.println("x="+x+",y="+y); // 6,5 ``` ```java= int x = 5;int y = 5; if (x++==5 | ++y==5){ x=11; } System.out.println("x="+x+",y="+y); // 11,6 ``` ```java= int x = 5;int y = 5; if (x++==5 || ++y==5){ x=11; } System.out.println("x="+x+",y="+y); // 11,5 ``` --- ```java= boolean x = true; boolean y = false; short z = 46; if ((z++==46)&&(y=true))z++; if ((x=false)||(++z==49))z++; System.out.println("z="+z); // 50 ``` # 賦值運算符 ![](https://i.imgur.com/3CWcZpO.png) - 複合運算符會進行類型轉換 ```java= byte b = 3; b += 2; // 有效,等價於 b = (byte)(b+2); b = b+2; // 但直接這樣寫會報錯 ``` # 三元運算符 (Ternary Operator) ![](https://i.imgur.com/s7RImTn.jpg) ## 課堂練習 - 實現三個數的最大值 ```java= public class TernaryOperatorExercise{ public static void main (String[] args){ int n1 = 55; int n2 = 33; int n3 = 123; int max1 = n1 > n2 ? n1 : n2; int max2 = max1 > n3 ? max1 : n3; System.out.println("Max is "+ max2); // 使用一條語句實現,但推薦使用上面方式 (效率及可讀性皆較好) // 老師提示: 後面我們可以使用更好方法,比如排序 int max = (n1 > n2 ? n1 : n2) > n3 ? (n1 > n2 ? n1 : n2) : n3; System.out.println("Max is "+ max); } } ``` # 運算符優先級 ![](https://i.imgur.com/CNHT3cU.jpg) # 關鍵字 ![](https://i.imgur.com/8C18VhW.png) ![](https://i.imgur.com/zPhMgeW.png) # 保留字 ![](https://i.imgur.com/eEjccEc.png) # 鍵盤輸入 - 步驟 - 導入該類的所在包, java.util.* - 創建該類對象(聲明變量) - 調用裡面的功能 - 演示 : 需求為取得用戶資訊 ```java= import java.util.Scanner; // 表示把java.util下的Scanner類導入 public class Input{ public static void main (String[] args){ Scanner scanner = new Scanner(System.in); System.out.println("請輸入名字"); String name = scanner.next(); // 接收用戶輸入 System.out.println("請輸入年齡"); int age = scanner.nextInt(); // 接收用戶輸入 System.out.println("請輸入薪水"); double sal = scanner.nextDouble(); // 接收用戶輸入 System.out.println("人的信息如下,名字="+name+", 年齡="+age+ ", 薪水="+sal); } } ``` # 四種進制介紹 ![](https://i.imgur.com/cO78JF0.jpg) ## 十轉二 ![](https://i.imgur.com/WQTjcsG.jpg) ## 十轉八 ![](https://i.imgur.com/Vx30dE7.jpg) ## 十轉十六 ![](https://i.imgur.com/cOK53Et.jpg) ## 二轉八 ![](https://i.imgur.com/0ZVtY70.jpg) ## 二轉十六 ![](https://i.imgur.com/koiIuR9.jpg) ## 八轉二 ![](https://i.imgur.com/Xprmx6I.jpg) ## 十六轉二 ![](https://i.imgur.com/jQIwOxL.jpg) # 位運算符 ![](https://i.imgur.com/wdPs7qA.jpg) ![](https://i.imgur.com/CNgW63g.jpg) ## 原碼、反碼、補碼 (==重點、難點==) - ==補碼將正負數統一起來了== ![](https://i.imgur.com/4kJmjI4.png) ```java= // 位運算 public class bitOperator{ public static void main (String[] args){ // 看老師推導過程 // 1. 運算需要2的補碼 => 2 的原碼 00000000 00000000 00000000 00000010 // 2的補碼 => 00000000 00000000 00000000 00000010 // 2. 3的補碼 => 3 的原碼 00000000 00000000 00000000 00000011 // 3的補碼 => 00000000 00000000 00000000 00000011 // 3. 按位& // 00000000 00000000 00000000 00000010 // 00000000 00000000 00000000 00000011 // 00000000 00000000 00000000 00000010 => 運算後的補碼 // 因為正數(三碼合一),運算後的原碼 00000000 00000000 00000000 00000010 // 結果就是 2 System.out.println(2&3); // 2 // 推導 // 1. 有-2的原碼 => 10000000 00000000 00000000 00000010 // 2. -2的反碼 => 11111111 11111111 11111111 11111101 // 3. -2的補碼 => 11111111 11111111 11111111 11111110 // 4. ~-2操作 => 00000000 00000000 00000000 00000001 => 運算後的補碼 // 5. 運算後的原碼即補碼 => 00000000 00000000 00000000 00000001 => 1 System.out.println(~-2); // 1 // 1. 得到2的補碼(即原碼) => 00000000 00000000 00000000 00000010 // 2. ~2操作 => 11111111 11111111 11111111 11111101 (運算後的補碼,負數!!) // 3. 運算後的反碼 => 11111111 11111111 11111111 11111100 // 4. 運算後的原碼 => 10000000 00000000 00000000 00000011 => -3 System.out.println(~2); // -3 // 1. 先得到2的補碼 => 2 的原碼 00000000 00000000 00000000 00000010 // 2的補碼 => 00000000 00000000 00000000 00000010 // 2. 3的補碼 => 3 的原碼 00000000 00000000 00000000 00000011 // 3的補碼 => 00000000 00000000 00000000 00000011 // 3. 按位| // 00000000 00000000 00000000 00000010 // 00000000 00000000 00000000 00000011 // 00000000 00000000 00000000 00000011 => 運算後的補碼 // 因為正數(三碼合一),運算後的原碼 00000000 00000000 00000000 00000011 =>3 System.out.println(2|3); // 3 // 1. 先得到2的補碼 => 2 的原碼 00000000 00000000 00000000 00000010 // 2的補碼 => 00000000 00000000 00000000 00000010 // 2. 3的補碼 => 3 的原碼 00000000 00000000 00000000 00000011 // 3的補碼 => 00000000 00000000 00000000 00000011 // 3. 按位異或 // 00000000 00000000 00000000 00000010 // 00000000 00000000 00000000 00000011 // 00000000 00000000 00000000 00000001 => 運算後的補碼 // 因為正數(三碼合一),運算後的原碼 00000000 00000000 00000000 00000001 =>1 System.out.println(2^3); // 1 } } ``` ## 位運算思考題 ```java= int a = 1>>2; int b = -1>>2; //算術右移 int c = 1<<2; //算術左移 int d = -1<<2; int e = 3>>>2; // 無符號右移 ``` ```java= // 位運算 public class bitExercise2{ public static void main (String[] args){ // 原碼即補碼 => 00000000 00000000 00000000 00000001 // 右移兩位 => 00000000 00000000 00000000 00000000 => 0 System.out.println(1>>2); // 0 // 原碼 => 10000000 00000000 00000000 00000001 // 反碼 => 11111111 11111111 11111111 11111110 // 補碼 => 11111111 11111111 11111111 11111111 // 右移兩位 => 11111111 11111111 11111111 11111111 (運算補碼) // 運算反碼 => 11111111 11111111 11111111 11111110 // 運算原碼 => 10000000 00000000 00000000 00000001 => -1 // 00000000 00000000 00000000 00000000 => 0 System.out.println(-1>>2); //-1 System.out.println(1<<2); // 4 System.out.println(-1<<2); // -4 System.out.println(3>>>2); // 0 // 原碼 => 10000000 00000000 00000000 00000011 // 反碼 => 11111111 11111111 11111111 11111100 // 補碼 => 11111111 11111111 11111111 11111101 // 右移兩位 => 11111111 11111111 11111111 11111111 (運算補碼) // 運算反碼 => 11111111 11111111 11111111 11111110 // 運算原碼 => 10000000 00000000 00000000 00000001 => -1 System.out.println(-3>>2); // -1 // 原碼 => 10000000 00000000 00000000 00000011 // 反碼 => 11111111 11111111 11111111 11111100 // 補碼 => 11111111 11111111 11111111 11111101 // 右移兩位 => 00111111 11111111 11111111 11111111 (運算補碼) // 運算原碼 => 1073741823 System.out.println(-3>>>2); // 1073741823 } } ``` --- ```java= ~2 = // 按位取反 2&3 = // 2按位與3 2|3 = // 2按位或3 ~-5 = 13&7= 5|4= -3^3= // 按位異或 ``` ```java= // 位運算 public class bitExercise{ public static void main (String[] args){ // 1. -5 的原碼 => 10000000 00000000 00000000 00000101 // 2. -5 的反碼 => 11111111 11111111 11111111 11111010 // 3. -5 的補碼 => 11111111 11111111 11111111 11111011 // 4. 取反操作 => 00000000 00000000 00000000 00000100 => 運算後的補碼 // 5. 補碼正數即原碼 => 4 System.out.println(~-5); // 4 // 推導 // 1. 13的原碼(補碼) => 00000000 00000000 00000000 00001101 // 2. 7的原碼(補碼) => 00000000 00000000 00000000 00000111 // 3. 按位 & => 00000000 00000000 00000000 00000101 => 運算後的補碼 // 4. 補碼正數即原碼 => 5 System.out.println(13&7); // 5 // 1. 5 的原碼 => 00000000 00000000 00000000 00000101 // 2. 4 的原碼 => 00000000 00000000 00000000 00000100 // 3. 按位| => 00000000 00000000 00000000 00000101 // 4. 補碼正數即原碼 => 5 System.out.println(5|4); // -5 // 1. -3 的原碼 => 10000000 00000000 00000000 00000011 // 2. -3 的反碼 => 11111111 11111111 11111111 11111100 // 3. -3 的補碼 => 11111111 11111111 11111111 11111101 // 4. 3 的原碼 => 00000000 00000000 00000000 00000011 // 5. 異或操作 => 11111111 11111111 11111111 11111110 => (運算後的補碼,負數!!) // 6. 運算後的反碼 => 11111111 11111111 11111111 11111101 // 7. 運算後的原碼 => 10000000 00000000 00000000 00000010 => -2 System.out.println(-3^3); // -2 } } ``` # 本章作業 ```java= 10 / 3 = 3 10 / 5 = 2 10 % 2 = 0 -10.5 % 3 = -1.5 // 注意 : 有小數參與運算,結果是近似值 ``` --- ```java= int i = 66; System.out.println(++i+i); // Me : 134 ``` --- ![](https://i.imgur.com/GulmFHF.png) 我答 : D > C也是對的,double也能有後綴D 正解 : C,D ![](https://i.imgur.com/PE9kJfQ.png) --- 4. 試寫出將String轉換成double類型的語句,以及將char類型轉換成String的語句 - Double.parseDouble(String) - char+""

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully