kotlin-if

if条件语句

Kotlin中的条件语句主要有两种实现方式:if和when。

fun main() {     
    val a = 37     
    val b = 40     
    val value = largerNumber(a, b)     
    println("larger number is " + value) 
} 
fun largerNumber(num1: Int, num2: Int): Int {     
    var value = 0     
    if (num1 > num2) {         
        value = num1     
    } else {         
        value = num2     
    }     
    return value 
}

kotlin代码中能存在返回值

简化代码 :

fun largerNumber(num1: Int, num2: Int): Int {     
    val value = if (num1 > num2) {         
        num1     //直接返回了num1
    } else {         
        num2     
    }     
    return value 
}

直接将if语句返回

fun largerNumber(num1: Int, num2: Int): Int {     
   return if (num1 > num2) {         
        num1     
    } else {         
        num2     
    }     
}

语法糖

fun largerNumber(num1: Int, num2: Int): Int = if(num1 > num2) num1 else num2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This error message indicates that there is a conflict between different variants of the `org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0` dependency that your project is trying to use. The consumer was configured to find a runtime of a component compatible with Java 11, packaged as a jar, and its dependencies declared externally, but it cannot choose between the available variants of the dependency. To resolve this issue, you can try the following steps: - Check if your project is explicitly declaring a version of the `org.jetbrains.kotlin:kotlin-gradle-plugin` dependency that conflicts with the version required by the consumer (in this case, Java 11). If so, try updating the project to use the correct version of the dependency. - If the above step does not work, you can try adding a resolution strategy to your build script to force Gradle to choose the correct variant of the dependency. For example, you can add the following code to your `build.gradle` file: ``` configurations.all { resolutionStrategy { eachDependency { DependencyResolveDetails details -> if (details.requested.group == 'org.jetbrains.kotlin' && details.requested.name == 'kotlin-gradle-plugin' && details.requested.version == '1.7.0') { details.useVersion('1.7.0') } } } } ``` This code tells Gradle to use version `1.7.0` of the `org.jetbrains.kotlin:kotlin-gradle-plugin` dependency whenever it is required, regardless of any conflicting versions that may be present. If neither of these steps work, you may need to investigate further to find the root cause of the conflict.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值