switch example



Write a program that allows the user to enter the grade scored in a class (0 - 100). If the user scored a 100, then notify that they got a perfect score.

Modify the prorgam so that if the user scored a 90 - 100, it informs the user that they scored an A.

Modify the program so that it will notify the user of their letter grade... 0 - 59: F, 60 - 69: D, 70 - 79: C, 80 - 89: B, 90 - 100: A

package main
import "fmt"

func main() {
    var score float64
    fmt.Println("Enter score for your last exam: ")
    fmt.Scanf("%v", &score)
    switch {
    case score <= 59:
        fmt.Println("Your grade is F")
    case score <= 69:
        fmt.Println("Your grade is C")
    case score <= 79:
        fmt.Println("Your grade is D")
    case score <= 89:
        fmt.Println("Your grade is B")
    case score <= 100:
        fmt.Println("Your grade is A")
    default:
        fmt.Println("Please enter a score <= 100")
    }
}

* The issue with this script is that if user enters a string, they still get a grade (F). What I want to happen is that the script will tell the user to input a number.

I will update this later...

No comments:

Post a Comment