refactor: retirando validação desnecessária e alterando if/else para switch case

This commit is contained in:
2026-07-11 14:47:29 -03:00
parent c509f6dc6a
commit 30995e55d3
2 changed files with 63 additions and 22 deletions
+16 -22
View File
@@ -9,14 +9,17 @@ import (
)
func main() {
numero, tentativas := 7, 3
const TENTATIVAS = 3
numero := 7
scanner := bufio.NewScanner(os.Stdin)
fmt.Println("Bem-vindo ao jogo de adivinhação!")
fmt.Printf("Você tem %d tentativas para adivinhar o número entre 1 e 10.\n", tentativas)
fmt.Printf("Você tem %d tentativas para adivinhar o número entre 1 e 10.\n", TENTATIVAS)
for i := 0; i <= tentativas; i++ {
if i == tentativas {
tentativa := 0
for tentativa < TENTATIVAS {
if tentativa == TENTATIVAS {
fmt.Println("Suas tentativas acabaram. O número correto era:", numero)
break
}
@@ -27,26 +30,17 @@ func main() {
if err != nil {
fmt.Println("Deve ser informado um número.")
continue
continue // colocado para pular a proxima iteração e não contar a tentativa
}
// talvez não necessite checar se o número é menor que um
//
if chuteInt < 1 {
fmt.Println("O número deve ser maior que 0.")
continue
}
if chuteInt < numero {
fmt.Println("O número é maior")
}
if chuteInt > numero {
fmt.Println("O número é menor")
}
if chuteInt == numero {
switch {
case chuteInt < numero:
fmt.Println("O número é maior, tente novamente.")
case chuteInt > numero:
fmt.Println("O número é menor, tente novamente.")
default:
fmt.Println("Parabéns! Você acertou o número!")
break
}
tentativa++
}
}