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.

變數ab,要計算它們的和,並將和儲存在另一個名為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.

螢幕上的widthheight以畫素為單位,計算畫素總數後,要將結果儲存在一個名為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.

兩個數字的sumdifference找出原始數字的值,並將其儲存在變數ab

我承認這題是邊看解答邊寫的(舉白旗 🏳️)看了看 感覺懂了又好像沒懂

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 areaof 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 12years 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.

班上男女百分比

參考提示作答的

(汗 終於

--

--