简体   繁体   English

如何在 kotlin 中定义 Long Long 数据类型?

[英]How to define Long Long data type in kotlin?

fun main(args: Array<String>) {
    try {
        var sum: Long = 0
        val n: Int = readLine()!!.toInt()

        for (i in 0..(n - 1)) {
            var input: Long?
            input = readLine()!!.toLong()
            sum += input
        }
        println(sum)
    } catch (ex: Exception) {
        println(ex.message)
    } 
}

I want to take data Type Long Long replacing at Long .我想在 Long 替换 Long Long 数据类型。 So how can I define Long Long data type?那么如何定义 Long Long 数据类型?

Kotlin's Long is 64-bit already. Kotlin 的Long已经是 64 位的了。 No need for ancient long long trickery:无需古long long诡计:

https://kotlinlang.org/docs/reference/basic-types.html https://kotlinlang.org/docs/reference/basic-types.html

If you're on the JVM, there isn't a long long type, but you could use java.math.BigInteger for arbitrarily large numbers instead.如果您在 JVM 上,则没有long long类型,但您可以使用java.math.BigInteger来代替任意大的数字。

See more discussion on this topic and some more alternatives at a Java question here .此处查看有关此主题的更多讨论以及 Java 问题中的更多替代方法。

Kotlin handles long long data type with the BigInteger data type. Kotlin 使用 BigInteger 数据类型处理 long long 数据类型。 Replace the long with BigInteger;用 BigInteger 替换 long;

fun main(args: Array<String>) {
    try {
        var sum = 0.toBigInteger()
        val n: Int = readLine()!!.toInt()

        for (i in 0..(n - 1)) {
            var input: BigInteger?
            input = readLine()!!.toBigInteger()
            sum += input
        }
        println(sum)
    } catch (ex: Exception) {
        println(ex.message)
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM