#6 Swift 的變數和常數練習( variable & constant)

參考學長姐的題目(遮住答案)自己來練習~

  • Sum
    You are given two variables a and b, compute their sum and store it in another variable named sum then print the result.
  • Second
    Determine the number of seconds in a year and store the number in a variable named secondsInAYear.
  • 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.
  • 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.
  • 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.
  • 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.
  • Last digit
    You are given a number a. Print the last digit of a.
  • 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 be y times older than her brother Bob. Bob is 12 years old. How many years does Alice have?
  • 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.
  • 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.

作業來源:

參考
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

--

--