#6 Swift 的變數和常數練習( variable & constant)
Published in
3 min readApr 1, 2024
參考學長姐的題目(遮住答案)自己來練習~
- Sum
You are given two variablesa
andb
, compute their sum and store it in another variable namedsum
then print the result.
- Second
Determine the number of seconds in a year and store the number in a variable namedsecondsInAYear
.
- Pixels
Your are given thewidth
andheight
of a screen in pixels. Calculate the total number of pixels on the screen and store the result in a variable namednumberOfPixels
.
- Sum and Difference
You are given thesum
and thedifference
of two numbers. Find out the values of the original numbers and store them in variablesa
andb
.
- L Area
You are given four variableswidth
,height
,x
,y
that describe the dimensions of a L-shape as shown in the image below. Determine theperimeter
andarea
of the described L-shape. Store the value of the perimeter in a variable namedperimeter
, and the area in a variable namedarea
.
- Swap
Given two variablea
andb
, swap their values. That is the new value ofa
will become the old value ofb
and vice versa.
- Last digit
You are given a numbera
. Print the last digit ofa
.
- 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.
- 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 bey
times older than her brother Bob. Bob is12
years old. How many years does Alice have?
- Apples and Oranges
You havex
apples. Bob trades3
oranges for5
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 namedapples
. The number of oranges you will have after the trade should be stored in a variable namedoranges
.
- Boys and Girls
A class consists ofnumberOfBoys
boys andnumberOfGirls
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 example33.333333333333
will be printed as33
.
作業來源:
參考
1. https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/5-swift-%E7%9A%84%E8%AE%8A%E6%95%B8%E5%92%8C%E5%B8%B8%E6%95%B8%E7%B7%B4%E7%BF%92-variable-constant-473be7ecfb77