Instantly share code, notes, and snippets.
Last active
February 13, 2017 14:00
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save titandiaz/083b767bd79afd68bf90b618d80d4bff to your computer and use it in GitHub Desktop.
Menu Switch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class MenuEstructurado { | |
static Scanner scanner = new Scanner(System.in); //Sirve para recoger texto por consola | |
static int select = -1; //opción elegida del usuario | |
static int base, altura, area, perimetro, radio, ladoEquilatero, baseRectangulo, alturaRectangulo; //Variables | |
static double areaCirculo, perimetroCirculo, areaEquilatero, perimetroEquilatero, areaRectangulo, hipotenusaRectangulo, perimetroRectangulo; | |
public static void main(String[] args) { | |
//Mientras la opción elegida sea 0, preguntamos al usuario | |
while(select != 0){ | |
//Try catch para evitar que el programa termine si hay un error | |
try{ | |
System.out.println("************************\nElige opción:\n1.- Rectangulo" + | |
"\n2.- Circulo\n" + | |
"3.- Triangulo Equilatero\n" + | |
"4.- Triangulo Rectangulo\n" + | |
"0.- Salir\n************************"); | |
//Recoger una variable por consola | |
select = Integer.parseInt(scanner.nextLine()); | |
//Switch case en Java | |
switch(select){ | |
case 1: | |
pideNumeros(); | |
areaRectangulo(base, altura); | |
System.out.print("El area del rectangulo es: " + area); | |
System.out.println(); | |
perimetroRectangulo(base, altura); | |
System.out.print("El perimetro del rectangulo es: " + perimetro); | |
System.out.println(); | |
break; | |
case 2: | |
pedirRadio(); | |
areaCirculo(radio); | |
System.out.print("El area del circulo es: " + areaCirculo); | |
System.out.println(); | |
perimetroCirculo(radio); | |
System.out.print("El perimetro del circulo es: " + perimetroCirculo); | |
System.out.println(); | |
break; | |
case 3: | |
pedirLado(); | |
areaEquilatero(ladoEquilatero); | |
System.out.print("El area del Triangulo Equilatero es: " + areaEquilatero); | |
System.out.println(); | |
perimetroEquilatero(ladoEquilatero); | |
System.out.print("El perimetro del Triangulo Equilatero es: " + perimetroEquilatero); | |
System.out.println(); | |
break; | |
case 4: | |
pedirAlturaBase(); | |
areaRectangulo(baseRectangulo, alturaRectangulo ); | |
System.out.print("El area del Triangulo Rectangulo es: " + areaRectangulo); | |
System.out.println(); | |
perimetroRectangulo(baseRectangulo, alturaRectangulo); | |
System.out.print("El perimetro del Triangulo Rectangulo es: " + perimetroRectangulo); | |
break; | |
case 0: | |
System.out.println("Adios!"); | |
break; | |
default: | |
System.out.println("Número no reconocido");break; | |
} | |
System.out.println("\n"); | |
}catch(Exception e){ | |
System.out.println("Uoop! Error!"); | |
} | |
} | |
} | |
public static void pideNumeros(){ | |
System.out.println("Digite la base del rectangulo: "); | |
base = Integer.parseInt(scanner.nextLine()); | |
System.out.println("Digite la altura del rectangulo: "); | |
altura = Integer.parseInt(scanner.nextLine()); | |
System.out.println(); | |
} | |
public static double areaRectangulo(int base, int altura){ | |
area = base*altura; | |
return area; | |
} | |
public static double perimetroRectangulo(int base, int altura){ | |
perimetro = (base*2)+(altura*2); | |
return perimetro; | |
} | |
public static void pedirRadio(){ | |
System.out.println("Digite el radio del circulo: "); | |
radio = Integer.parseInt(scanner.nextLine()); | |
System.out.println(); | |
} | |
public static double areaCirculo(int radio){ | |
areaCirculo = Math.PI*(radio*radio); | |
return areaCirculo; | |
} | |
public static double perimetroCirculo(int radio){ | |
perimetroCirculo = 2*Math.PI*radio; | |
return perimetroCirculo; | |
} | |
public static void pedirLado(){ | |
System.out.println("Digite el lado del Triangulo Equilatero: "); | |
ladoEquilatero = Integer.parseInt(scanner.nextLine()); | |
System.out.println(); | |
} | |
public static double areaEquilatero(int ladoEquilatero){ | |
areaEquilatero = (Math.pow(ladoEquilatero,2)*Math.sqrt(3))/4; | |
return areaEquilatero; | |
} | |
public static double perimetroEquilatero(double ladoEquilatero){ | |
perimetroEquilatero = 3*ladoEquilatero; | |
return perimetroEquilatero; | |
} | |
public static void pedirAlturaBase(){ | |
System.out.println("Digite la base del triangulo rectangulo: "); | |
baseRectangulo = Integer.parseInt(scanner.nextLine()); | |
System.out.println("Digite la altura del triangulo rectangulo: "); | |
alturaRectangulo = Integer.parseInt(scanner.nextLine()); | |
System.out.println(); | |
} | |
public static double areaTrianguloRectangulo(int baseRectangulo, int alturaRectangulo){ | |
areaRectangulo = (baseRectangulo*alturaRectangulo)/2; | |
return areaRectangulo; | |
} | |
public static double perimetroTrianguloRectangulo(int baseRectangulo, int alturaRectangulo){ | |
hipotenusaRectangulo = Math.sqrt((Math.pow(baseRectangulo, 2)+ Math.pow(alturaRectangulo, 2) )); | |
perimetroRectangulo = alturaRectangulo + baseRectangulo + hipotenusaRectangulo; | |
return perimetroRectangulo; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment