Home > Backend Development > Golang > How to add exclude negative numbers

How to add exclude negative numbers

PHPz
Release: 2024-02-09 23:30:09
forward
589 people have browsed it

How to add exclude negative numbers

php Xiaobian Yuzai introduces you how to add a method to exclude negative numbers in programming. In some cases, we need to restrict the input values ​​to ensure that they are not negative. This is especially important in scenarios where positive values ​​are required, such as money or age. We can achieve this easily by using conditional statements and mathematical functions. Next, we'll discuss in detail how to add methods to exclude negative numbers in different programming languages ​​to help you solve this common problem.

Question content

<code>
Write a program that queries the user for three positive integers and
displays messages:

All numbers are equal if all three numbers are equal

Two numbers are equal if any two numbers out of three are equal

All numbers are different if all three numbers entered are different


If there is an error in the input data, e.g., negative numbers are
entered, the program should display the message Invalid Input
</code>
Copy after login
package main

import (
    "fmt"
)

func main() {
    var a, b, c int
    fmt.Scan(&a)
    fmt.Scan(&b)
    fmt.Scan(&c)
    if (a != b && a > 0) && (b != c && c > 0) && (a != c && b > 0) {
        fmt.Print("All the numbers are different")
    } else if a == b && b == a && a == c && c == b {
        fmt.Print("All numbers are equal")
    } else if (a == b && b > 0) || (b == a && a > 0) || (c == a && a > 0) || (c == b && b > 0) {
        fmt.Print("Two numbers are equal")
    } else {
        fmt.Print("Invalid request")
    }
}
Copy after login

How can I make the program output the wrong query if I enter for example -4 -4 -4?

Solution

Use one set. The idiomatic way to implement collections in Go is to use maps.

Then check the length of the collection.

package main

import (
    "fmt"
)

func main() {

    var a int = 5
    var b int = 9
    var c int = 5

    fmt.Scan(&a)
    fmt.Scan(&b)
    fmt.Scan(&c)

    s := map[int]bool{a: true, b: true, c: true}

    if a < 0 || b < 0 || c < 0 {
        fmt.Print("Invalid request")
    } else if len(s) == 3 {
        fmt.Print("All the numbers are different")
    } else if len(s) == 2 {
        fmt.Print("Two numbers are equal")
    } else if len(s) == 1 {
        fmt.Print("All numbers are equal")
    }

}
Copy after login

The above is the detailed content of How to add exclude negative numbers. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template