9.md 3.7 KB
Newer Older
W
wizardforcel 已提交
1 2 3 4 5 6 7 8
# Kotlin 基本输入/输出

> 原文: [https://www.programiz.com/kotlin-programming/input-output](https://www.programiz.com/kotlin-programming/input-output)

#### 在本文中,您将学习在屏幕上显示输出,并在 Kotlin 中接受用户的输入。

## Koltin 输出

W
wizardforcel 已提交
9
您可以使用`println()``print()`函数将输出发送到标准输出(屏幕)。 让我们举个例子:
W
wizardforcel 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

```kt
fun main(args : Array<String>) {
    println("Kotlin is interesting.")
}
```

运行该程序时,输出为:

```kt
Kotlin is interesting.

```

此处,`println()`输出字符串(引号内)。

* * *

W
wizardforcel 已提交
28
### `println()`和`print()`之间的区别
W
wizardforcel 已提交
29

W
wizardforcel 已提交
30 31
*   `print()` - 在引号内打印字符串。
*   `println()` - 在引号内打印字符串,类似于`print()`函数。 然后光标移动到下一行的开头。
W
wizardforcel 已提交
32 33 34 35 36

* * *

使用`println()`函数时,它将在内部调用`System.out.println()`函数。 (`System.out.println()`用于将输出打印到 Java 屏幕中)。

W
wizardforcel 已提交
37
如果您使用的是 IntelliJ IDEA,请将鼠标光标放在`println`旁边,然后转到`Navigate > Declaration`(快捷键:`Ctrl + B`。对于 Mac:`Cmd + B`),这将打开`Console.kt`(声明文件)。 您会看到`println()`函数正在内部调用`System.out.println()`
W
wizardforcel 已提交
38 39 40 41 42

同样,当您使用`print()`函数时,它将调用`System.out.print()`函数。

* * *

W
wizardforcel 已提交
43
### 示例 1:`print()`和`println()`
W
wizardforcel 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

```kt
fun main(args : Array<String>) {
    println("1\. println ");
    println("2\. println ");

    print("1\. print ");
    print("2\. print");
}
```

When you run the program, the output will be:

```kt
1\. println 
2\. println 
1\. print 2\. print
```

* * *

W
wizardforcel 已提交
65
### 示例 2:打印变量和字面值
W
wizardforcel 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

```kt
fun main(args : Array<String>) {
    val score = 12.3

    println("score")
    println("$score")
    println("score = $score")
    println("${score + score}")
    println(12.3)
}
```

When you run the program, the output will be:

```kt
score
12.3
score = 12.3
24.6
12.3
```

* * *

## Kotlin 输入

在本节中,您将学习从用户那里接受输入。

要在 Kotlin 中读取一行字符串,可以使用`readline()`函数。

* * *

W
wizardforcel 已提交
99
### 示例 3:打印用户输入的字符串
W
wizardforcel 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

```kt
fun main(args: Array<String>) {
    print("Enter text: ")

    val stringInput = readLine()!!
    println("You entered: $stringInput")
}
```

When you run the program, the output will be:

```kt
Enter text: Hmm, interesting!
You entered: Hmm, interesting!
```

可以使用`readLine()`函数将输入作为字符串,然后将其显式转换为其他数据类型(如`Int`)的值。

* * *

如果要输入其他数据类型,则可以使用`Scanner`对象。

为此,您需要使用以下方法从 Java 标准库中导入`Scanner`类:

```kt
import java.util.Scanner 
```

W
wizardforcel 已提交
129
然后,您需要从此类创建`Scanner`对象。
W
wizardforcel 已提交
130 131 132 133 134

```kt
val reader = Scanner(System.`in`) 
```

W
wizardforcel 已提交
135
现在,`reader`对象用于接收用户的输入。
W
wizardforcel 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

* * *

### 示例 4:从用户获取整数输入

```kt
import java.util.Scanner

fun main(args: Array<String>) {

    // Creates an instance which takes input from standard input (keyboard)
    val reader = Scanner(System.`in`)
    print("Enter a number: ")

    // nextInt() reads the next integer from the keyboard
    var integer:Int = reader.nextInt()

    println("You entered: $integer")
}
```

When you run the program, the output will be:

```kt
Enter a number: -12
You entered: -12
```

W
wizardforcel 已提交
164
此处,创建了`Scanner`类的`reader`对象。 然后,调用`nextInt()`方法,该方法从用户那里获取整数输入,该整数输入存储在变量`integer`中。
W
wizardforcel 已提交
165 166 167 168

* * *

要从用户获取`Long``Float``double``Boolean`输入,可以分别使用`nextLong()``nextFloat()``nextDouble()``nextBoolean()`方法。