改进翻译

Getting Started with IntelliJ IDEA

最近更新 2020-07-07
This tutorial demonstrates how to use IntelliJ IDEA for creating a console application.

To get started, first download and install the latest version of IntelliJ IDEA.

Create an application

Once you've installed IntelliJ IDEA, it's time to create your first Kotlin application.

  1. In IntelliJ IDEA, select File | New | Project.
  2. In the panel on the left, select Kotlin.
  3. Enter a project name, select Console Application as the project template, and click Next.

    Create a console application

    By default, your project will use the Gradle build system with Kotlin DSL.

  4. Go through and accept the default configuration, then click Finish.

    Configure a console application

    Your project will open. By default, you see the file build.gradle.kts, which is the build script created by the Project Wizard based on your configuration. It includes the kotlin("jvm") plugin and dependencies required for your console application.

  5. Open the main.kt file in src/main/kotlin.
    The src directory contains Kotlin source files and resources. The main.kt file contains sample code that will print Hello World!.

    main.kt with main fun

  6. Modify the code so that it requests your name and says Hello to you specifically, and not to the whole world.

    • Introduce a local variable name with the keyword val. It will get its value from an input where you will enter your name – readLine().
    • Use a string template by adding a dollar sign $ before this variable name directly in the text output like this – $name.
    fun main() {
        println("What's your name?")
        val name= readLine()
        println("Hello $name!")
    }
    

    Updated main fun

Run the application

Now the application is ready to run. The easiest way to do this is to click the green Run icon in the gutter and select Run 'MainKt'.

Running a console app

You can see the result in the Run tool window.

Kotlin run output

Enter your name and accept the greetings from your application!

Kotlin run output

恭喜! 你现在运行了第一个 Kotlin 应用程序。

What's next?

Once you’ve created this application, you can start to dive deeper into Kotlin syntax: