Introduce una temperatura en grados Celsius o Fahrenheit:
import java.util.Scanner;
public class ConversorTemperaturas {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Bienvenido al Conversor de Temperaturas");
System.out.print("Por favor, introduce una temperatura (por ejemplo, 25°C o 75°F): ");
String entrada = scanner.nextLine();
if (entrada.matches("^\\d+(\\.\\d+)?°?[CFcf]$")) {
char unidad = Character.toUpperCase(entrada.charAt(entrada.length() - 1));
double valor = Double.parseDouble(entrada.replaceAll("[^0-9.]", ""));
if (unidad == 'C') {
double fahrenheit = (valor * 9/5) + 32;
System.out.printf("%.2f°C es igual a %.2f°F%n", valor, fahrenheit);
} else if (unidad == 'F') {
double celsius = (valor - 32) * 5/9;
System.out.printf("%.2f°F es igual a %.2f°C%n", valor, celsius);
}
} else {
System.out.println("Formato de temperatura no válido. Debe ser en °C o °F.");
}
scanner.close();
}
}