diff --git a/src/P112_RadaresDeTramo.java b/src/P112_RadaresDeTramo.java index 8117fdf..938562f 100644 --- a/src/P112_RadaresDeTramo.java +++ b/src/P112_RadaresDeTramo.java @@ -1,6 +1,6 @@ import java.util.Scanner; -public class RadaresDeTramo { +public class P112_RadaresDeTramo { public static void main() { Scanner sc = new Scanner(System.in); int distancia = 0; // Distancia entre las cámaras (metros) @@ -8,11 +8,15 @@ public class RadaresDeTramo { int tiempo = 0; // Tiempo que tarda el coche en pasar entre las cámaras (segundos) while(true) { // Condición de salida después de obtener datos - System.out.print("Introduce los datos: "); distancia = sc.nextInt(); velPermitida = sc.nextInt(); tiempo = sc.nextInt(); sc.nextLine(); // Limpiar stdin + // Validar que los datos sean mayores que 0 + if (distancia < 0 || velPermitida < 0 || tiempo < 0) { + System.out.println("ERROR"); + continue; + } // Salir del bucle if (distancia == 0 && velPermitida == 0 && tiempo == 0) { System.out.println("SALIR"); diff --git a/src/P164_AreaRectangulo.java b/src/P164_AreaRectangulo.java index 3267d67..93128a5 100644 --- a/src/P164_AreaRectangulo.java +++ b/src/P164_AreaRectangulo.java @@ -1,13 +1,23 @@ -//TIP To Run code, press or -// click the icon in the gutter. -void main() { - //TIP Press with your caret at the highlighted text - // to see how IntelliJ IDEA suggests fixing it. - IO.println(String.format("Hello and welcome!")); +import java.util.Scanner; - for (int i = 1; i <= 5; i++) { - //TIP Press to start debugging your code. We have set one breakpoint - // for you, but you can always add more by pressing . - IO.println("i = " + i); - } -} +void main() { + Scanner sc = new Scanner(System.in); + // Coordenadas de las esquinas opuestas + int ax, ay, bx, by; + // Longitud de lados + int l1, l2; + // Area del rectangulo + int area; + while(true) { + System.out.print("Introduce las coordenadas del rectángulo (ax ay bx by): "); + ax = sc.nextInt(); + ay = sc.nextInt(); + bx = sc.nextInt(); + by = sc.nextInt(); + // Ternario para obtener resta positiva + l1 = (ax < bx) ? bx - ax : ax - bx; + l2 = (ay < by) ? by - ay : ay - by; + area = l1 * l2; + System.out.println("El área es: " + area); + } +} \ No newline at end of file