Java Online Compiler
Example: Check Number Sign with If-Else in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Check Number Sign with If-Else import java.util.Scanner; // Main class containing the entry point of the program public class Main { public static void main(String[] args) { // Step 1: Create a Scanner object to read user input Scanner scanner = new Scanner(System.in); // Step 2: Prompt the user to enter a number System.out.print("Enter a number: "); double number = scanner.nextDouble(); // Read the number (using double for flexibility) // Step 3: Check the sign of the number using if-else if-else if (number > 0) { System.out.println(number + " is a positive number."); } else if (number < 0) { System.out.println(number + " is a negative number."); } else { System.out.println(number + " is zero."); } // Step 4: Close the scanner scanner.close(); } }
Output
Clear
ADVERTISEMENTS