Ejemplo switch case Calculando Grados Fahrenheit Celsius y Kelvin Java
En este ejemplo de java utilizaremos Las Formulas de conversión de incrementos
de grados Fahrenheit Celsius y Kelvin para calcular el valor de temperatura Introducido utilizando el switch.
Formulas:
Fahrenheit a Celsius C =(F-32)*5/9
Fahrenheit a Kelvin K = (F-32)*5/9 +273
Celsius a Fahrenheit F =C*9/5+32
Celsius a Kelvin K=C+273
Kelvin a Fahrenheit F= (k-273)9/5+32
Kelvin a Celsius C=K-273
Código:
import java.util.Scanner;
public class Conversiones{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
double F,C,K; String result;
int accion;
accion=1;
F =0;
C=0;
K=0;
try{
while(accion==1 || accion==2 || accion==3 || accion==4 || accion==5 || accion==6){
System.out.println("\nSeleccione la conversion que quiere haser");
System.out.println("1. Fahrenheit a Celsius");
System.out.println("2. Fahrenheit a Kelvin");
System.out.println("3. Celsius a Farenheit");
System.out.println("4. Celius a Kelvin");
System.out.println("5. Kelvin a Fahrenheit");
System.out.println("6. Kelvin a Celsius");
accion = sc.nextInt();
if(accion!=1 && accion!=2 && accion!=3 && accion!=4 && accion!=5 && accion!=6){
System.out.println("Error: ingrese el numero de alguna accion"); }///fin del if
switch(accion){
case 1 : // Fahrenheit a Celsius
// C =(F-32)*5/9
sc.nextDouble();
System.out.println("Ingrese cantidad de Fahrenheit para convertir a Celsius");
F=0;
try{ F=sc.nextDouble();} catch(Exception e){
System.out.println("Error no ingreso un numero");}
C =(F-32)*5/9;
System.out.print("El total de celsius es: "+C);
break;
case 2 ://Fahrenheit a Kelvin
//K = (F-32)*5/9 +273
System.out.println("Ingrese cantidad de Fahrenheit para convertir a Kelvin");
F=0;
try{ F=sc.nextDouble();} catch(Exception e){//fin del try
System.out.println("Error no ingreso un numero");}
K = (F-32)*5/9 +273;
System.out.print("El total de Kelvin es: "+K);
break;
case 3 : //Celsius a Fahrenheit
//F =C*9/5+32
System.out.println("Ingrese cantidad de Celsius para convertir a Farenheit");
C=0;
try{ C=sc.nextInt();} catch(Exception e){//fin del try
System.out.println("Error no ingreso un numero");}
F =C*9/5+32;
System.out.print("El total de Farenheit es: "+F);
break;
case 4 ://Celsius a Kelvin K=C+273
System.out.println("Ingrese cantidad de Celsius para convertir a Kelvin");
C=0;
try{ C=sc.nextDouble();} catch(Exception e){//fin del try
System.out.println("Error no ingreso un numero");}
K=C+273;
System.out.print("El total de Kelvin es: "+K);
break;
case 5 : // Kelvin a Fahrenheit F= (k-273)9/5+32
System.out.println("Ingrese cantidad de Kelvin para convertir a Fahrenheit");
K=0;
try{ K=sc.nextDouble();} catch(Exception e){//fin del try
System.out.println("Error no ingreso un numero");}
F= (K-273)*9/5+32;
System.out.print("El total de Fahrenheit es: "+F);
break;
case 6 :// Kelvin a Celsius C=K-273
System.out.println("Ingrese cantidad de Kelvin para convertir a Celsius");
K=0;
try{ K=sc.nextDouble();} catch(Exception e){//fin del try
System.out.println("Error no ingreso un numero");}
C=K-273;
System.out.print("El total de Celsius es: "+C);
break;
default:
result="mmmmmmmmm";
}
}
} catch(Exception e){//fin del try
accion =0;
System.out.println("Error no ingreso un numero correcto");
}//fin del catch
}
}
Descargar Ejemplo:

Comentarios
Publicar un comentario