English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Contrôle de flux Java

Java Tableau

Java Programmation Orientée Objet (I)

Java Programmation Orientée Objet (II)

Java Programmation Orientée Objet (III)

Java Exception Handling

Java Liste (List)

Java Queue (file d'attente)

Java Map Collection

Java Set Collection

Java Entrée Sortie (I/O)

Java Reader/Writer

Autres sujets Java

Programme Java pour vérifier si un nombre est positif ou négatif

Java complete list of examples

Dans ce programme, vous allez apprendre à vérifier si un nombre donné est positif ou négatif. Cela est réalisé en utilisant des instructions if else en Java.

Exemple : Utilisation de if else pour vérifier si un nombre est positif ou négatif

public class PositiveNegative {
    public static void main(String[] args) {
        double number = 12.3;
        //If number is less than 0, it is true
        if (number < 0.0)
            System.out.println(number + " is a negative number.");
        //If number is greater than 0, return true
        else if ( number > 0.0)
            System.out.println(number + " is a positive number.");
        // If both test expressions are calculated as false
        else
            System.out.println(number + " is 0.");
    }
}

When running the program, the output is:

12.3 It is a positive number.

If the value is changed to a negative number (for example-12.3),then the output is:

-12.3 It is a negative number.

In the above program, by comparing the variable number with 0, it can be clearly seen whether the variable number is positive or negative.

If uncertain, please follow the following steps:

  • If the number is greater than zero, it is a positive number.

  • If the number is less than zero, it is a negative number.

  • If the number is zero, it is zero.

Java complete list of examples