12.0-12.1

This commit is contained in:
Unknwon
2015-11-11 12:47:10 -05:00
parent 719824b26d
commit 522e63960b
6 changed files with 14 additions and 10 deletions

View File

@@ -71,6 +71,7 @@ func main() {
屏幕是标准输出 `os.Stdout``os.Stderr` 用于显示错误信息,大多数情况下等同于 `os.Stdout`
一般情况下,我们会省略变量声明,而使用 `:=`,例如:
```go
inputReader := bufio.NewReader(os.Stdin)
input, err := inputReader.ReadString('\n')
@@ -81,6 +82,7 @@ input, err := inputReader.ReadString('\n')
第二个例子从键盘读取输入,使用了 `switch` 语句:
示例 12.3 [switch_input.go](examples/chapter_12/switch_input.go)
```go
package main
import (
@@ -93,12 +95,12 @@ func main() {
inputReader := bufio.NewReader(os.Stdin)
fmt.Println("Please enter your name:")
input, err := inputReader.ReadString('\n')
if err != nil {
fmt.Println("There were errors reading, exiting program.")
return
}
fmt.Printf("Your name is %s", input)
// For Unix: test with delimiter "\n", for Windows: test with "\r\n"
switch input {
@@ -107,7 +109,7 @@ func main() {
case "Ivo\r\n": fmt.Println("Welcome Ivo!")
default: fmt.Printf("You are not welcome here! Goodbye!")
}
// version 2:
switch input {
case "Philip\r\n": fallthrough
@@ -115,7 +117,7 @@ func main() {
case "Chris\r\n": fmt.Printf("Welcome %s\n", input)
default: fmt.Printf("You are not welcome here! Goodbye!\n")
}
// version 3:
switch input {
case "Philip\r\n", "Ivo\r\n": fmt.Printf("Welcome %s\n", input)
@@ -137,11 +139,10 @@ iii) 输入的行数
**练习 12.2:** [calculator.go](exercises/chapter_12/calculator.go)
编写一个简单的逆波兰式计算器,它接受用户输入的整型数(最大值 999999和运算符 +、-、*、/。
编写一个简单的逆波兰式计算器,它接受用户输入的整型数(最大值 999999和运算符 +、-、\*、/。
输入的格式为number1 ENTER number2 ENTER operator ENTER --> 显示结果
当用户输入字符 'q' 时程序结束。请使用您在练习11.3中开发的 `stack` 包。
## 链接
- [目录](directory.md)