finish homework
This commit is contained in:
parent
d2a06bbb34
commit
988a239192
|
@ -2,12 +2,51 @@
|
||||||
//
|
//
|
||||||
// SPDX-License-Identifier: BSD-3-Clause
|
// SPDX-License-Identifier: BSD-3-Clause
|
||||||
|
|
||||||
|
// Write a program that lets the user input a fruit (Apple, Banana, Strawberry).
|
||||||
|
// Depending on the input, let the program output a fun fact about the fruit.
|
||||||
|
// Ask the user if he wants to input another fruit, and if so let him do so.
|
||||||
|
// Think about what kind of variables you need and in which level to write them.
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("Hello world!")
|
prompt()
|
||||||
|
}
|
||||||
|
|
||||||
|
func printFact(s string) {
|
||||||
|
if s == "a" {
|
||||||
|
fmt.Println("Some apple varieties such as Pink Pearl and Kissabel have flesh that ranges from pink or orange to bright red.")
|
||||||
|
} else if s == "b" {
|
||||||
|
fmt.Println("Bananas don’t actually grow on trees—they grow on plants that are officially classified as an herb (not surprisingly, the world’s largest herb). They’re in the same family as lilies, orchids, and palms.")
|
||||||
|
} else if s == "s" {
|
||||||
|
fmt.Println("The strawberry is a member of rose family and it is the only fruit which has seeds outside.")
|
||||||
|
} else {
|
||||||
|
fmt.Println("Please choose one of [a]pple, [b]anana, or [s]trawberry.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func loop() {
|
||||||
|
fmt.Print("Would you like to hear another fact? [y/n]: ")
|
||||||
|
var s string
|
||||||
|
fmt.Scanln(&s)
|
||||||
|
if s == "y" {
|
||||||
|
prompt()
|
||||||
|
} else if s == "n" {
|
||||||
|
os.Exit(0)
|
||||||
|
} else {
|
||||||
|
fmt.Println("Please enter either [y]es or [n]o.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func prompt() {
|
||||||
|
fmt.Print("Please select a fruit to hear an interesting fact about ([a]pple, [b]anana, [s]trawberry): ")
|
||||||
|
var s string
|
||||||
|
fmt.Scanln(&s)
|
||||||
|
printFact(s)
|
||||||
|
loop()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue