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 两个数字相加

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐