薰Shin
  • NEW!
    NEW!  Connect Ideas Across Notes
    Save time and share insights. With Paragraph Citation, you can quote others’ work with source info built in. If someone cites your note, you’ll see a card showing where it’s used—bringing notes closer together.
    Got it
      • 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 No publishing access yet

        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.

        Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

        Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

        Explore these features while you wait
        Complete general settings
        Bookmark and like published notes
        Write a few more notes
        Complete general settings
        Write a few more notes
        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 No publishing access yet

    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.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    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
    # Java第九週[JPA306~310 while迴圈+副程式運用、continue運用、do_while運用] #### 複習用 ## 開始解題(JPA306~310): ### 第一題(JPA306)-迴圈階乘計算[(不斷執行,直到條件不成立跳出)]: #### 計算公式: [${m^n=m*m*m*....共n次}$] 已知n,可設for迴圈,讓他跑n次 然後m乘上他自己即可 但因為在輸入時,m值會改變,所以要再設一個變數承接 ```java=1 for(int i=1;i<=n;i++) //跑n次 { int ans=1; ans*=m; } ``` #### 題目解答問題處: [${100^7}=100000000000000$] 但解答為[${100^7}=276447232$] 是因為int溢位導致,就是超出本身int能計算的範圍 所以結果才是276447232,但我們題庫為此結果,所以不做處理。(參考資料2為處理教學) {%youtube q-G7YOae1Xw %} [參考資料](https://happyplayblogs.blogspot.com/2015/05/tqc-java6-306.html) [參考資料2](https://java.4-x.tw/java-03-2) ![](https://i.imgur.com/UfGJwr9.png) 解答: ```java import java.util.*; public class JPA306 { public static void main (String argv[]) { int num1, num2; Scanner input = new Scanner(System.in); System.out.println("Input:"); num1 = input.nextInt(); while (num1 != 999) //當num1=999跳出(條件) { num2 = input.nextInt(); System.out.println(powerOf(num1, num2)); System.out.println("Input:");//自打 num1 = input.nextInt();//自打 } } static int powerOf(int m, int n) { //自打 int result=1; for(int i=1;i<=n;i++) { result*=m; //一定要一個ans或result承接 } //若用m*=m;會多成一個m值 return result; //因為本來的m=1被蓋掉成m=2 } } ``` --- ### 第二題(JPA307)-迴圈最大公因數[使用輾轉消除法]: #### 輾轉消除法(歐幾里得算法) 求[最大公因數]的算法,可藉此公式快速算出 **兩個整數的最大公因數=其中較小的數(n)&兩數相除餘數的最大公因數(m%n)** 剩下 **還沒有變成零的數(m)** 就是兩數的最大公因數 ```java=1 ans=m%n; //兩數相除餘數的最大公因數 m=n; //其中較小的數(假設m為較大) n=ans; //再把餘數給n //形成m一定最大,n最小,直到n=0就跳出(用while設條件) ``` ![](https://i.imgur.com/rtH04xn.png) 解答: ```java import java.util.Scanner; //自打 public class JPA307 { public static void main (String argv[]) { int num1, num2; Scanner keyboard=new Scanner(System.in); //自打 System.out.println("Input:"); num1=keyboard.nextInt(); while (num1!=999) //自打 { num2=keyboard.nextInt(); System.out.println(gcd(num1,num2)); System.out.println("Input:"); num1=keyboard.nextInt(); } } static int gcd(int m, int n) { int result=0; //自打 while(n!=0) { result=m%n; m=n; n=result; } return m; } } ``` --- ### 第三題(JPA308)-電腦週邊費用總計[使用do_while迴圈&總和]: * 注意!!!記得要多判斷 ```java=1 if(s!=-1) { tatol+=s; //避免多加-1,導致結果不對 } ``` ![](https://i.imgur.com/F9KIplp.png) ![](https://i.imgur.com/NYIjpnQ.png) 解答: ```java import java.util.Scanner; public class JPA308 { static Scanner keyboard = new Scanner(System.in); static int i = -1; //用不到 public static void main(String[] args) { int total = 0, s = 0; do { System.out.print("請輸入消費金額,或輸入-1結束:"); s=keyboard.nextInt(); if(s!=-1) { total+=s; } }while(s!=-1); //s=-1就跳出(結束) System.out.println("電腦週邊總消費:"+total); } } ``` --- ### 第四題(JPA309)-迴圈倍數判斷[continue運用]: #### continue和break的差別 continue作用與break類似,使用於**迴圈** **break**會結束區塊執行(**直接跳出**) **continue**只會**略過**之後陳述句,並回到迴圈區塊開頭**進行下一次迴圈**,而**不是離開迴圈** **參考文章:** [第一篇](https://tubobo.net/jobindex/2020/08/29/%E3%80%90java%E3%80%91return-break-continue-%E5%B7%AE%E5%88%A5/) [第二篇](https://openhome.cc/Gossip/Java/Break-Continue.html) * **注意!!!迴圈mod時,不是n%,而是 i%,這樣才能跑迴圈** ![](https://i.imgur.com/efvtmzS.png) 解答: ```java import java.util.Scanner; class JPA309 { public static void main(String argv[]) { int n,sum=0; Scanner keyboard=new Scanner(System.in); n=keyboard.nextInt(); for(int i=1;i<=n;i++) { if(i%7==0) { continue; //跳下一個迴圈(忽略本身執行的迴圈) } else if((i%3==0)||(i%5==0)) { sum+=i; } } System.out.println("Answer: " + sum); } } ``` --- ### 第五題(JPA310)-迴圈正偶數相加[do_while迴圈運用]: #### do_while迴圈 while為 **成立就繼續跑(不成立跳出)** 題目條件為 n>0、n%2==0 當n=3,3>0,但不是偶數 所以要用||(or),不用&&(and) 若我們要繼續跑第一個while迴圈,讓他重複執行 直到結果為 **正偶數** 才能跑第二個迴圈(去計算相加) ![](https://i.imgur.com/qQAXZyg.png) 解答: ```java import java.util.Scanner; public class JPA310 { static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { int n=0; do { System.out.print("請輸入n的值(n>0,且為偶數):"); n=keyboard.nextInt(); }while((n<=0)||(n%2!=0)); int sum=0,i=2; do { sum+=i; i+=2; }while(i<=n); System.out.printf("2+4+...+%d=%d\n",n,sum); } } ``` --- 最後編輯時間:2021/4/28 12:56pm. ###### tags: `JAVA課堂學習` `複習用` `高科大`

    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
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    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