03 Swift 的變數和常數練習( variable & constant )🧠
感覺要進入可怕的邏輯漩渦了,怕自己跟不上進度,課後複習不敢馬乎😗
練習題:
https://www.weheartswift.com/variables-constants-basic-operations/
1.1 Sum
You are given two variables a
and b
, compute their sum and store it in another variable named sum
then print the result.
變數a
和b
,要計算它們的和,並將和儲存在另一個名為sum
的變數中,然後列印
1.2 Seconds
Determine the number of seconds in a year and store the number in a variable named secondsInAYear
.
計算一年有幾秒鐘呢?
1.3 Pixels
Your are given the width
and height
of a screen in pixels. Calculate the total number of pixels on the screen and store the result in a variable named numberOfPixels
.
螢幕上的width
和height
以畫素為單位,計算畫素總數後,要將結果儲存在一個名為numberOfPixels
的變數中
1.4 Sum and Difference
You are given the sum
and the difference
of two numbers. Find out the values of the original numbers and store them in variables a
and b
.
兩個數字的sum
和difference
找出原始數字的值,並將其儲存在變數a
和b
。
1.5 L Area
You are given four variables width
, height
, x
, y
that describe the dimensions of a L-shape as shown in the image below. Determine the perimeter
and area
of the described L-shape. Store the value of the perimeter in a variable named perimeter
, and the area in a variable named area
.
求周長及面積
1.6 Swap
Given two variable a
and b
, swap their values. That is the new value of a
will become the old value of b
and vice versa.
交換a跟b的值(這題上課時peter有教到)
1.7 Last digit
You are given a number a
. Print the last digit of a
.
給你一個數字a
列印a
的最後一位數字。
1.8 Dog Years
You are given Rocky’s age in dog years. Print Rocky’s age in human years. You know that 1 human year is 7 dog years.
狗狗在人類的年紀是幾歲呢?人類1歲是狗的7歲
1.9 Brothers
Everyone hates solving word problems by hand so let’s make a program to solve them for us.x
years from now Alice will be y
times older than her brother Bob. Bob is 12
years old. How many years does Alice have?
愛麗絲與鮑伯年紀
1.10 Apples and Oranges
You have x
apples. Bob trades 3
oranges for 5
apples. He does not accept trades with cut fruit.
How many oranges can you get from Bob and how many apples will you have left?
The number of apples you will have left should be stored in a variable named apples
. The number of oranges you will have after the trade should be stored in a variable named oranges
.
你有x
個蘋果 鮑勃用3
橘子換5
個蘋果。 他不換切好的水果。
你能從鮑勃那裡得到多少個橘子,還剩下多少個蘋果?
剩餘的蘋果數量儲存在一個名為apples
變數中
交換後將擁有的柳丁數量儲存在名為oranges
變數中
1.11 Boys and Girls
A class consists of numberOfBoys
boys and numberOfGirls
girls.
Print the percentage of boys in the class followed by the percentage of girls in the class. The percentage should be printed rounded down to the nearest integer. For example 33.333333333333
will be printed as 33
.
班上男女百分比
(汗 終於