斯威夫特山地車
In this program, we will have an idea - how two numbers can be added and displayed as the output on the screen?
在此程序中,我們將有一個想法- 如何將兩個數字相加并顯示為屏幕上的輸出 ?
Open XCode terminal and type the following program
打開XCode終端并鍵入以下程序
import Foundation
var num1 = 4;
var num2 = 6;
var ans = Int();
ans = num1+num2;
print("num1 + num2 = ",ans);
Output
輸出量
num1 + num2 = 10
Program ended with exit code: 0
Explanation:
說明:
import Foundation is a library which is used to access all the functions present that header file.
import Foundation是一個庫,用于訪問該頭文件中提供的所有功能。
var is a data type with num1 as a variable whose value is initialized as 4. Similarly, num2 is a variable with same data type whose value is initialized as 6.
var是一種數據類型,以num1作為變量,其值初始化為4 。 同樣, num2是具有相同數據類型的變量,其值初始化為6 。
Whereas, var ans includes same data type with ans as a variable where the result is stored.
而var ans包含與ans相同的數據類型,而ans作為存儲結果的變量。
print function prints the string with the answer on the output screen.
打印功能在輸出屏幕上打印帶有答案的字符串。
Whereas, "program ended with exit code: 0" at the end shows that there are no further programs to be run.
而結尾處的“程序以退出代碼0結尾”表示沒有其他程序要運行。
Semicolon ";" is must after every end of the command as similar in C/C++ etc.
分號“;” 在命令的每個結尾之后必須是,類似于C / C ++等。
翻譯自: https://www.includehelp.com/swift/addition-of-two-numbers.aspx
斯威夫特山地車