50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"fmt"
|
||
|
"math"
|
||
|
"math/rand"
|
||
|
"os"
|
||
|
"strconv"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
for {
|
||
|
rand.Seed(time.Now().UnixNano())
|
||
|
fmt.Println("Train")
|
||
|
fmt.Println("Creative Computing Morristown, New Jersey")
|
||
|
fmt.Printf("Time - Speed Distance Exercise:")
|
||
|
var car int = rand.Intn(65-40) + 40
|
||
|
var distance int = rand.Intn(20-5) + 20
|
||
|
var train int = rand.Intn(39-20) + 39
|
||
|
fmt.Printf(" A Car traveling %d MPH can make a certain trip in %d hours less than a Train traveling at %d MPH.\n", car, distance, train)
|
||
|
for {
|
||
|
scanner := bufio.NewScanner(os.Stdin)
|
||
|
fmt.Printf("\nHow long does the trip take by car ")
|
||
|
scanner.Scan()
|
||
|
answer, err := strconv.ParseFloat(scanner.Text(), 64)
|
||
|
if err != nil {
|
||
|
fmt.Println("Please enter a number")
|
||
|
continue
|
||
|
}
|
||
|
var car_time int = distance * train / (car - train)
|
||
|
var percent int = int(math.Abs((float64(car_time)-answer)*100/answer) + .5)
|
||
|
if percent > 5 {
|
||
|
fmt.Printf("Sorry, you were off by %d percent\n", percent)
|
||
|
fmt.Printf("Correct answer is %d hours\n", car_time)
|
||
|
} else {
|
||
|
fmt.Printf("Good! Answer within %d percent\n", percent)
|
||
|
fmt.Printf("Another Problem? (yes or no) ")
|
||
|
scanner.Scan()
|
||
|
if scanner.Text() == "yes" {
|
||
|
break
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
continue
|
||
|
}
|
||
|
}
|
||
|
}
|