kotlin 兩個數字相加
Given two numbers, we have to swap them.
給定兩個數字,我們必須交換它們。
Example:
例:
Input:
First number: 10
Second number: 20
Output:
First number: 20
Second number: 10
To swap two numbers – here we are using third variable/temporary variable (consider – first contains the first number, second contains the second number and temp is a temporary variable).
要交換兩個數字 –在這里,我們使用第三個變量/臨時變量(考慮– 第一個包含第一個數字, 第二個包含第二個數字,而temp是一個臨時變量)。
Assign the first number (first) to the temporary variable (temp)
將第一個數字( first )分配給臨時變量( temp )
Now, assign the second number (second) to the variable first.
現在,將第二個數字( second )分配給變量first 。
Now, assign the temp (that contains the first number) to the second.
現在,將溫度 (包含第一個數字)分配給第二個 。
Finally, values are swapped, print them on the screen.
最后,交換值,然后將其打印在屏幕上。
計劃在Kotlin交換兩個數字 (Program to swap two numbers in Kotlin)
package com.includehelp.basic
import java.util.*
// Main Method Entry Point of Program
fun main(arg: Array<String>) {
// InputStream to get Input
var reader = Scanner(System.`in`)
// Input two values
println("Enter First Value : ")
var first = reader.nextInt();
println("Enter Second Value : ")
var second = reader.nextInt();
println("Numbers Before Swap : \n first = $first \n second = $second ")
//Code for Swap Numbers
var temp = first
first=second
second=temp
println("Numbers After Swap : \n first = $first \n second = $second ")
}
Output
輸出量
Run 1:
Enter First Value :
45
Enter Second Value :
12
Numbers Before Swap :
first = 45
second = 12
Numbers After Swap :
first = 12
second = 45
翻譯自: https://www.includehelp.com/kotlin/swap-two-numbers.aspx
kotlin 兩個數字相加