-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (71 loc) · 1.58 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
type Player struct {
Name string
Score int
}
var players []Player
func main() {
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Println("1. Add Player")
fmt.Println("2. Update Score")
fmt.Println("3. Display Scores")
fmt.Println("4. Exit")
fmt.Print("Enter your choice: ")
scanner.Scan()
choice := scanner.Text()
switch choice {
case "1":
addPlayer(scanner)
case "2":
updateScore(scanner)
case "3":
displayScores()
case "4":
fmt.Println("Exiting...")
return
default:
fmt.Println("Invalid choice. Please try again.")
}
}
}
func addPlayer(scanner *bufio.Scanner) {
fmt.Print("Enter player name: ")
scanner.Scan()
name := scanner.Text()
players = append(players, Player{Name: name, Score: 0})
fmt.Println("Player added successfully.")
}
func updateScore(scanner *bufio.Scanner) {
fmt.Print("Enter player name: ")
scanner.Scan()
name := scanner.Text()
for i, player := range players {
if player.Name == name {
fmt.Print("Enter score to add: ")
scanner.Scan()
scoreInput := scanner.Text()
score, err := strconv.Atoi(scoreInput)
if err != nil {
fmt.Println("Invalid score. Please enter a number.")
return
}
players[i].Score += score
fmt.Println("Score updated successfully.")
return
}
}
fmt.Println("Player not found.")
}
func displayScores() {
fmt.Println("Current Scores:")
for _, player := range players {
fmt.Printf("%s: %d\n", player.Name, player.Score)
}
}