變數與選擇判斷 靜宜大學資管系 楊子青
1. 變數(Variables) A variable is a named storage location, that stores a value of a particular data type. It is called variable because you can change the value stored. App Inventor不用 設定資料型態, 會自動進行轉換
(1) 宣告變數 1. 拖曳出 initialize global name to 全域數域 區域數域
宣告變數 2. 設定變數名稱 3. 設定初值
(2) 取得變數值 選擇變數名稱
(3) 設定(變更)變數值 選擇變數名稱
Setting a Variable to an Expression Incrementing a Variable Building Complex Expressions
Math drawer In the Math drawer, App Inventor provides a wide range of mathematical functions.
(4) 顯示變數值 Changing a variable has no direct effect on the app’s appearance. When you modify a component property, the user interface is directly affected. This is not true for variables. This is one of the advantages of storing data in a variable as opposed to a component property it allows you to show just the data you want when you want to show it. It also allows you to separate the computational part of your app from the user interface, making it easier to change that user interface later.
2. 選擇判斷 App Inventor provides conditional blocks which are found in the Control drawer of the Built-In palette.
Relational and logical operator blocks used in conditional tests You can plug any Boolean expression into the “test” slot of these blocks.
鱷魚拔牙整人玩具 http://www.i-gamer.net/play/2978.html
修改專案,短按圖片第6次出現獅子
修改專案,隨機於第2-6次出現獅子
自行練習:請設計一個擲筊的App 擲筊(擲爻) 凸起部份稱為「陰面」(也就是反面),平面的部份則稱為「陽面」(也就是正面)。 會出現三種情況: 聖杯:一正一反,表示神明同意你的請求 笑杯:兩個平面向上,表示神明在笑,還未決定同不同意 蓋杯(陰杯):兩個平面朝下,表示請求駁回
提示 拉一個Button,讓使用者開始擲筊 設定變數Divination1, Divination2,以及Rnd 假設1表示正面:Divination變數值設為True 假設2表示背面:Divination變數值設為False 先用一個Label,暫時顯示兩個筊杯的數字 根據Divination1, Divination2判斷擲筊結果,於手機螢幕顯示出相關的圖片
執行實例
練習: 結合加速器,一次擲一個筊杯 擲完兩次之後,再判斷結果 (聖杯/笑杯/蓋杯) 練習: 結合加速器,一次擲一個筊杯 擲完兩次之後,再判斷結果 (聖杯/笑杯/蓋杯)