Kotlin ,IntelliJ : math operator not working

标签 kotlin intellij-idea

  • Ubuntu 伴侣 20.04
  • IntelliJ IDEA Community 2020.2 EAP,通过 Snap 安装
  • Kotlin 1.3.72,通过 Snap 安装
  • JRE 1.8.0_242-8u242-b08-0ubuntu3~16.04-b08
  • 项目设置为 Kotlin:使用 SDK 11 (java 11.0.6) 的 JVM|IDEA

代码:

fun main(args: Array<String>) {
    var experiencePoints: Int = 5
    experiencePoints += 5
    println(experiencePoints)
}

在“+=”下方有一条红色波浪线并产生以下错误:

Error:(3, 22) Kotlin: Cannot access 'java.io.Serializable' which is a supertype of 'kotlin.Int'. Check your module classpath for missing or conflicting dependencies

将代码更改为以下内容:

fun main(args: Array<String>) {
    var experiencePoints: Int = 5
    experiencePoints = experiencePoints + 5
    println(experiencePoints)
}

“+”字符下方的红色波浪线和相同的错误。

这对我来说是第 1 天,遵循 Skeen 和 Greenhalgh 编写的“Kotlin 编程,大 Nerd 牧场指南”中的示例。不知道如何处理这个错误。

最佳答案

我在 MacOS Catalina 上的 Intellij 2021.2 Ultimate 中有这个,我确实安装了 JDK。但是在 File -> Project Structure -> Project Settings -> Project -> Project SDK 我选择了 Kotlin SDK。有道理,这是一个 Kotlin 项目,对吧?错了,这里需要选择Java JDK。

同样在平台设置 -> SDKs -> Kotlin SDK 下,我缺少类路径。我不确定这是否有必要,但当我从头开始创建一个新的 Kotlin 项目时就是这样。我在导入现有的 Java/Kotlin 组合项目时遇到了问题。

关于 Kotlin ,IntelliJ : math operator not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62124681/

相关文章:

intellij-idea - IntelliJ IDEA : how to find all instance creation of a class?

google-app-engine - 我可以使用 intellj app-engine 插件将我的 grails 应用程序部署到 app-engine

java - java fxml中mysql的面积图

java - Kotlin 使用的 Java 库中的泛型和继承

kotlin - "experimental"Kotlin协程可以在生产中使用吗?

android - float 操作按钮 : Setting a custom background is not supported

java - 智能 : Easier way to check if a maven artifact is included in the project?

intellij-idea - "In order to run a story file you need to first set a main class in the JBehave settings"intellij

嵌套成员的 Kotlin 可见性

android - 在 Android (Kotlin) 中处理单例清理的推荐方法是什么?