張世旭
    • 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 New
    • Engagement control
    • Make a copy
    • 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 Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 簡易計算機OnAction事件 * 參考bmi按鈕 OnAction事件 設定方法: ```java= btn.setOnAction( new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent t) { String str1=itext1.getText(); String str2=itext2.getText(); double h=Double.parseDouble(str1); double w=Double.parseDouble(str2); h=h/100.0; // cm--> m double bmi=w/(h*h); label3.setText("BMI值:"+ bmi); } } ); ``` # 可改成Lambda表達式 ```java btn.setOnAction( (t)-> { //也可 t -> {body} String str1=itext1.getText(); String str2=itext2.getText(); double h=Double.parseDouble(str1); double w=Double.parseDouble(str2); h=h/100.0; // cm--> m double bmi=w/(h*h); label3.setText("BMI值:"+ bmi); } ); ``` # 宣告成Global ```java Button btn0, btn1, btn2, btn3, btn4, btn5; Button btn6, btn7, btn8, btn9, btnDot, btnC; Button btnPlus, btnMin, btnMult, btnDiv, btnCal; TextField itext; double d1=0, d2=0, result=0; int flag=0; ``` ```java= itext=new TextField(); HBox hbox1=new HBox(10); HBox hbox2=new HBox(10); HBox hbox3=new HBox(10); HBox hbox4=new HBox(10); VBox vbox=new VBox(10); btn0=new Button("0"); btn1=new Button("1"); btn2=new Button("2"); btn3=new Button("3"); btn4=new Button("4"); btn5=new Button("5"); btn6=new Button("6"); btn7=new Button("7"); btn8=new Button("8"); btn9=new Button("9"); btnDot=new Button("."); btnC=new Button("C"); btnPlus=new Button("+"); btnMin=new Button("-"); btnMult=new Button("*"); btnDiv=new Button("/"); btnCal=new Button(" = "); initAction(); ``` ```java void initAction(){ btn0.setOnAction(e->{}); 點擊1按鈕時 textField內容要加"1" 1. 要取得textField的內容 2. str2 取到內容後加1.. 3. 判斷str2是否合乎數字.. 若合乎數字 才設定設定textField的內容 } ``` ```java void initAction(){ btn1.setOnAction(e->{ //1. 要取得textField的內容 String str1=itext.getText(); //2. 取到內容後加1.. String str2=str1 +"1"; try{ Double.parseDouble(str2); //3. 設定textField的內容. itext.setText(str2); } catch(Exception ee){} }); btnDot.setOnAction(e->{ String str1=itext.getText(); //1. 要取得textField的內容 String str2=str1 +".";//2. 取到內容後加1.. try{ Double.parseDouble(str2); itext.setText(str2);//3. 設定textField的內容. } catch(Exception ee){} }); //按C 清除 btnC.setOnAction(e->{ itext.setText(""); }); } 按 alt+shift+F 格式化你的程式碼 ``` # 把btn改成btn0, btn1, ..., btn9..等等. ```java= btn0.setOnAction( (t)-> { //改成 t-->{body} 也可. //itext加入"0", 步驟如下: //1.取得目前itext中的內容字串 String old=itext.getText(); //2.將 old字串後面加上"0". String str=old ..... //將新字串str設給itext. itext.setText(.....); } ); btn1.setOnAction( (t)-> { //itext加入"1" //1.取得目前itext中的內容字串 String old=itext.getText(); //2.將 old字串後面加上"1". String str=old ..... //將新字串str設給itext. itext.setText(.....); } ); btn2.setOnAction( (t)-> { //itext加入"2" //1.取得目前itext中的內容字串 String old=itext.getText(); //2.將 old字串後面加上"2". String str=old ..... //將新字串str設給itext. itext.setText(.....); } ); ... btn9.setOnAction( (t)-> { //itext加入"9" } ); btnDot.setOnAction( (t)-> { //itext加入"." } ); btnC.setOnAction( (t)-> { //itext清除程式碼 itext.SetText(""); } ); btnPlus.setOnAction( (t)-> { //設定 加法運算 } ); btnMin.setOnAction( (t)-> { //設定 減法運算 } ); btnMult.setOnAction( (t)-> { //設定 乘法運算 } ); btnDiv.setOnAction( (t)-> { //設定 除法運算 } ); btnCal.setOnAction( (t)-> { // = 計算運算 } ); ``` # 設全域變數 存下操作的中間過程 double d1, d2, result; ```java 例如 按 +,-,* / 之前的字串或數字. 例如 itext目前內容為 12 當按下 "+"號時, 要存下 12, 並將itext內容清空. <== d1內容應為12 ``` # Double.parseDouble(字串) ```java 將字串轉為浮點數, 可使用Double.parseDouble(itext.getText()) d1=Double.parseDouble(itext.getText()); ``` # 考慮按 "="號的動作: ```java 當按下 "="號時, 假設目前itext內容為10 <== d2內容應為10 則計算 d1+d2 結果為22 將結果顯示於itext. ``` # 問題: 當按下 "="號, 怎知是做那種運算? 因之前有按 "+"號, 因此按 "+"號時 要作記號. # 使用全域變數 flag存下運算符號. int flag=0; ```java 當按下 "+"號時, flag 設成0, 代表 "+"號 <-btnPlus Action事件 當按下 "-"號時, flag 設成1, 代表 "-"號 <-btnMin Action事件 當按下 "*"號時, flag 設成2, 代表 "*"號 <-btnMult Action事件 當按下 "/"號時, flag 設成3, 代表 "/"號 <-btnDiv Action事件 ``` ```java btnCal.setOnAction(e->{ String str1=itext.getText(); d2=Double.parseDouble(str1); if(flag==0) result=d1+d2; else if(flag==1) result=d1-d2; else if(flag==2) result=d1*d2; else{ //flag=3 /除法 可能出錯. if(d2==0) return; //0不做除法 result=d1/d2; } itext.setText(""+result); }); ``` <hr> ```java btnPlus.setOnAction( (t)-> { //設定 加法運算 //1.要存下 12, 並將itext內容清空 String str1=itext.getText(); itext.setText(""); d1=Double.parseDouble(str1); flag=0; //加. } ); ``` <hr> ```java btnCal.setOnAction( (t)-> { //= 計算運算 } ); ```

    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