Ejercicio
Uno o dos números negativos
Objetivo
Cree un programa en java para aceptar dos números del usuario y responder si ambos son negativos, si solo uno lo es o si ninguno de ellos lo es.
Código de Ejemplo
import java.util.Scanner;
public class Program {
// Main entry point of the program
public static void main(String[] args) {
// Create a scanner to read user input
Scanner scanner = new Scanner(System.in);
// Declare variables to store the two numbers entered by the user
int num1, num2;
// Prompt the user to enter the first number
System.out.print("Enter the first number: ");
num1 = scanner.nextInt();
// Prompt the user to enter the second number
System.out.print("Enter the second number: ");
num2 = scanner.nextInt();
// Check the conditions for both numbers being negative, one negative, or neither
if (num1 < 0 && num2 < 0) {
// If both numbers are negative, display this message
System.out.println("Both numbers are negative.");
} else if (num1 < 0 || num2 < 0) {
// If only one of the numbers is negative, display this message
System.out.println("One number is negative.");
} else {
// If neither of the numbers is negative, display this message
System.out.println("Neither of the numbers is negative.");
}
}
}