I can't figure out how to build the code correctly. Please help me dumb(
Here is the task itself
Write a program that uses the number entered to determine which of the four stacks it should be placed in. The program asks the user for a number and displays a message:
1. The number is negative and even if the number is less than zero and is an even number
2. The number is negative and odd if the number is less than zero and is an odd number
3. a number is positive and even if the number is greater than zero and is an even number
4. A number is positive and odd if the number is greater than zero and is an odd number
I tried if and else
Also I don't understand if it is possible to execute with - switch?
It's already a headache. The only thing I can do is to define integers and non integers. I don't understand what to add to the code to define negative and positive numbers.
package main
import (
"fmt"
)
func main() {
var score int
fmt.Scanln(&score)
if score%2 == 0 && score < 0 {
fmt.Println("The number is negative and even")
} else {
fmt.Println("The number is negative and not even")
}
}
Why when entering a positive number, the program still writes that the number is negative
Because I specify that a<0
Please help me
Your program will classify all numbers you input as 'negative' because there is no print statement in your program that prints the word 'positive'.
You can use a different, but still rather simple approach to tackle this problem:
package main
import "fmt"
func main() {
var score int
_, err := fmt.Scanln(&score)
if err != nil {
panic(err)
}
// print whether the number is negative, zero, or positive, and also whether it is even or odd, in one line
// take note that fmt.Print is used here and not Println, so that it does not append a newline to the end of the string
if score < 0 {
fmt.Print("The number is negative")
} else if score == 0 {
fmt.Print("The number is zero")
} else {
fmt.Print("The number is positive")
}
if score%2 == 0 {
fmt.Println(" and even")
} else {
fmt.Println(" and odd")
}
}