forked from kant003/HolaMundoHacktoberfest2017
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConversorGrados.java
53 lines (44 loc) · 1.31 KB
/
ConversorGrados.java
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.Scanner;
public class Ejercicio4
{
// Método de Celsius a Farenheit
public static double CelsiusAFarenheit(double valor)
{
return (32 + (9 * (valor / 5)));
}
// Método de Farenheit a Celsius
public static double FarenheitACelsius(double valor)
{
valor = ((valor - 32) * 5 / 9);
return valor;
}
public static void main(String[] args)
{
/*
* Programa que lea una cantidad de grados centígrados y la pase a grados
* Fahrenheit.La fórmula correspondiente es:F = 32 + ( 9 * C / 5)
*/
// Scanner
Scanner sc = new Scanner(System.in);
// Pedir opcion
System.out.println("Escriba 1 si quiere convertir de grados Celsius a Farenheit.");
System.out.println("Escriba 2 si quiere convertir de grados Farenheit a Celsius.");
// Leer opcion
int opcion;
double grados;
do
{
opcion = sc.nextInt();
} while ((opcion != 1) && (opcion != 2));
System.out.println("Introduzca los grados " + ((opcion == 1) ? "Celsius" : "Farenheit"));
grados = sc.nextDouble();
// Si la opcion es 1 llamamos a metodo 1 y si es 2 al metodo 2.
if (opcion == 1)
{
System.out.println(grados + " Grados Celsius, son " + CelsiusAFarenheit(grados) + " Grados Farenheit");
} else
{
System.out.println(grados + " Grados Farenheit, son " + FarenheitACelsius(grados) + " Grados Celsius");
}
}
}