S K
    • 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
    ###### tags: `minecraft` <style> * {font-family: 'Terminal','ヒラギノ丸ゴ ProN','Hiragino Maru Gothic ProN',YuGothic,'Yu Gothic','Hiragino Kaku Gothic ProN','ヒラギノ角ゴ ProN W3','Verdana','Arial',sans-serif;} .markdown-body {font-family: 'Terminal','ヒラギノ丸ゴ ProN','Hiragino Maru Gothic ProN',YuGothic,'Yu Gothic','Hiragino Kaku Gothic ProN','ヒラギノ角ゴ ProN W3','Verdana','Arial',sans-serif;} h2 {padding-top: 50px;} img {margin: 20px auto;} .alert-success {margin-top: 50px;} .alert h2 {padding-top: 0px;} .part {letter-spacing: .1rem;} /* .markdown-body {max-width: 1024px;} */ .markdown-body h3 {margin-top: 12px;margin-bottom: 12px;} .markdown-body p {font-size: 1.25em;margin-top: 12px; margin-bottom: 16px;} .markdown-body li {font-size: 1.25em;} .markdown-body pre {font-size: 20px;} .focus {color: red;font-weight: bold;} .link-box {display: flex;justify-content: space-between; margin-top: 50px;margin-bottom: 50px;} a.link {display: block; width: 50%; padding: 10px 15px; text-align: center; color: #fff; font-weight: bold; font-size: 20px; letter-spacing: .1em; text-decoration: none;} a.link:hover {opacity: 0.85;} a.prev {background: linear-gradient(to bottom, #1A7BBD 0%, #1A7BBD 50%, #125684 51%, #125684 100%); border-radius: 10px 0 0 10px;} a.next {background: linear-gradient(to bottom, #D76820 0%, #D76820 50%, #964816 51%, #964816 100%); border-radius: 0 10px 10px 0;} </style> # 【第25回 タートルで計算してみよう!】 :::success ## 【今回の目標】 - **タートルで計算をしてみよう** - **計算と命令を組み合わせてみよう** ::: ## 5つの計算ができるよ! タートルには 足し算・引き算・かけ算・割り算 に加えて、「あまり(剰余)」 を求める 5つの計算 をさせることができるよ! ![](https://i.imgur.com/zxosWYm.png) ## プログラムでの書き方 それぞれの計算は、下のようにみんながいつも使っている記号とは、ちょっと違う記号を使うこともあるんだ! ![](https://i.imgur.com/aYswLkU.png) | 計算の種類 | 意味 | プログラムで使う記号 | 例(結果) | |:----------:|:---------:|:--------------------:|:----------:| | 足し算 | +(たす) | + | 1 + 2 → 3 | | 引き算 | −(ひく) | - | 5 - 2 → 3 | | 掛け算 | ×(かける | * | 3 * 4 → 12 | | 割り算 | ÷(わる) | / | 10 / 2 → 5 | | あまり | ÷のあまり | % | 10 % 3 → 1 | 「%」は「剰余(じょうよ)」っていって、 **割り算をしたときに残る「あまり」** を出してくれるよ! たとえば 10 % 3 は「3で割って1あまる」から「1」になるんだね! ## 変数を使って計算する **計算した結果は変数に入れることもできるよ!** **また、変数を使って計算することもできるんだ!** 例えば次のようにできるよ! ~~~ a = 1+2 ~~~ まず、変数 a に 1 + 2 の結果「3」を入れて、 ~~~ b = 3*a ~~~ 次に b に 3 * a の計算結果「9」を入れているよ! :::info 【クイズ!】 さて、b の変数に入っている数字はいくつかな? ついでに、次のクイズにも挑戦してみよう! ```lua= c = 10 % 3 ``` このとき、c はいくつになるかな? ヒント:10 ÷ 3 の「あまり」だよ! ::: ## 計算の順序 **計算の順序は算数と同じだよ!** **かっこの中身が先に計算されるから、かっこを使えば計算の順序を変えることができるね!** ![](https://i.imgur.com/cBQgbSm.png) :::success ## 課題1 : タートルに計算させてみよう! ::: ### 次の3つの式A, B, Cを計算させてみよう! ### 計算結果は print命令で表示させてね! ### $\quad$ A) 12345 + 54321 ### $\quad$ B) 256 × 256 ### $\quad$ C) 10 + 6 × 8 ### さあ、うまく計算できてるかな? --- :::success ## 課題2 : 1から100までの合計 ::: ### 1から100までの合計を求めて表示しよう! ### for文を使えばうまくできそうだね! ### $\quad$ 1 + 2 + 3 + 4 + 5 + $\cdots$ + 98 + 99 + 100 = ? :::info 【ヒント】 ### $\quad$ A = 10 ### $\quad$ A = A + 1 $\quad$ これは**変数Aのなかに変数A + 1の数値を入れるという意味** これと **for文** を組み合わせて、プログラムを完成させよう! ::: --- ## 課題6:偶数か奇数かを調べてみよう! 「%(あまり)」を使うと、ある数字が「偶数」か「奇数」かを調べることができるんだ! ### 【やってみよう】 次のようなルールで、タートルが数字を調べるプログラムを作ってみよう! - 数字を1つ入力する(io.read命令) - その数字が偶数なら「偶数です」と表示 - 奇数なら「奇数です」と表示 :::info 【ヒント】 tonumber(io.read()) で数字を入力できるよ 2で割ったあまりが「0」なら偶数、そうでなければ奇数だよ! ::: ```lua= num = tonumber(io.read()) -- 入力された文字を数字に変換 if num % 2 == 0 then print("偶数です") else print("奇数です") end ``` 【確認してみよう】 以下の数字で確認してみよう: - 8 → 「偶数です」 - 13 → 「奇数です」 - 0 → 「偶数です」 - ー5 → 「奇数です」 マイナスやゼロでもちゃんと動くかな?確認してみよう! ## 💡さらにチャレンジ! ## 課題7:1から20までの奇数だけを表示しよう! for文と % を組み合わせて、1〜20のうち奇数だけを print で表示するプログラムを書いてみよう! :::info 【ヒント】 ```lua= for i = 1, 20 do if i % 2 == 1 then print(i) end end ``` % を使って「奇数」のときだけ print するようにしてるよ! ::: ### 🎯 学習ポイントまとめ(追記用) %(あまり)を使うと、割り切れるかどうかをチェックできる 偶数・奇数の判定や、**ある回数ごとに何かをしたいとき(3回ごとなど)** によく使う! ## 条件分岐と一緒に使うと、より賢いプログラムが書けるようになる! ## 応用しよう! 計算の方法はわかったかな? 次はじっさいに命令と組み合わせて使ってみよう! :::success ## 課題3 : その場で回転させる ::: ### io.read 命令とかけ算を使って、数字を入力するとその回数だけタートルがその場で回転するプログラムを作ろう! ![](https://i.imgur.com/9sgmoGJ.png) :::info 【ヒント】 回転する向きは左回りでも右回りでもOK **io.read命令** を使って入力した数字は**文字の数**だから、**tonumber命令**を使って数字に変換して使おう! ::: ### きちんとかけ算を使ってかけたかな? --- :::success ## 課題4 : 持っているアイテムの個数の合計を調べよう! ::: ### turtle.getItemCount(スロット番号) 命令を使って、タートルが持っているアイテムの個数の合計を調べよう! ![](https://i.imgur.com/uGCTSkB.png) :::info 【ヒント】 プログラムで数を数えるときは、数えた数を保存するための変数を特別に用意するよ! ~~~lua= count = 0 -- 数えた数を保存するための変数、初めは0 for i = 1, 10 do -- for文を10回実行する count=count+1 -- for文1回ごとにcountに1ずつ加えていく end print(count) -- 10と表示される ~~~ ::: --- :::success ## 課題5 : 土ブロックの合計を調べよう! ::: ### 今度はタートルの持っている土ブロックの個数の合計を調べよう! ![](https://i.imgur.com/0IAMGOC.png) :::info 【ヒント】 - for文を使おう! - 土ブロックの名前は "minecraft:dirt" だよ! - アイテムの種類と個数を調べるために、 **turtle.getItemDetail(スロット番号)** 命令を使おう! - 変数は下のような **Table型** になっていて、**["name"]** でそのアイテムの名前を調べることができるよ! ![](https://i.imgur.com/njdEkFq.png) - 空のスロットを調べてしまうとエラーが出るよ! **nil** を使ってエラーを回避しよう! ![](https://i.imgur.com/J14T5gt.png) ![](https://i.imgur.com/q7TksAY.png) ::: --- ### うまくできたかな?わからないことは先生にどんどん聞いてみよう! <div class="link-box"> <a href="#" class="link prev">◀︎ 前の内容に戻る</a> <a href="#" class="link next">次の内容に進む ▶︎</a> </div>

    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