-
Lenguaje
Java usando Scanner
-
Descripción
Convertir grados Fahrenheit a Celsius y viceversa. El programa deberá leer en primer lugar un número entero que indicará si se va a convertir de grados Fahrenheit a Celsius (1) o de grados Celsius a Fahrenheit (2). Seguidamente leerá el valor de la temperatura, realizará la conversión aplicando la fórmula adecuada e imprimirá el resultado.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.Scanner;
public class FahrenheitACelsiusYViceversa {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int opcion;
double resultado, temperatura;
System.out.print("Ingresa el valor de temperatura: ");
temperatura = in.nextDouble();
in.nextLine();
System.out.println("Selecciona el valor de opcion.");
System.out.println("\t1.- Fahrenheit a Celsius");
System.out.println("\t2.- Celsius a Fahrenheit");
System.out.print("\t: ");
do {
opcion = in.nextInt();
in.nextLine();
if (opcion<1||opcion>2)
System.out.print("Valor incorrecto. Ingr\u00E9salo nuevamente.: ");
} while (opcion<1||opcion>2);
if(opcion==1)
resultado=(temperatura-32)/1.8;
else
resultado=1.8*temperatura+32;
System.out.println("Valor de resultado: " + resultado);
}
}
public class FahrenheitACelsiusYViceversa {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int opcion;
double resultado, temperatura;
System.out.print("Ingresa el valor de temperatura: ");
temperatura = in.nextDouble();
in.nextLine();
System.out.println("Selecciona el valor de opcion.");
System.out.println("\t1.- Fahrenheit a Celsius");
System.out.println("\t2.- Celsius a Fahrenheit");
System.out.print("\t: ");
do {
opcion = in.nextInt();
in.nextLine();
if (opcion<1||opcion>2)
System.out.print("Valor incorrecto. Ingr\u00E9salo nuevamente.: ");
} while (opcion<1||opcion>2);
if(opcion==1)
resultado=(temperatura-32)/1.8;
else
resultado=1.8*temperatura+32;
System.out.println("Valor de resultado: " + resultado);
}
}