OOP_IO-2x_2023-mirror/Java/lab_1/lab_1.java

35 lines
997 B
Java
Raw Normal View History

2023-03-22 17:02:31 +02:00
import java.util.Scanner;
2023-05-07 15:19:06 +03:00
public class lab_1 {
2023-05-07 22:20:31 +03:00
public static int protectedInput(String variableToRead, Scanner input) {
int readVariable;
2023-05-02 09:58:31 +03:00
do {
try {
2023-05-07 22:20:31 +03:00
System.out.printf("Enter %s: ", variableToRead);
readVariable = input.nextInt();
2023-05-02 09:58:31 +03:00
break;
} catch (Exception e) {
2023-05-07 22:20:31 +03:00
System.out.printf("%s must be an integer.\n", variableToRead.toUpperCase());
2023-05-02 09:58:31 +03:00
input.nextLine();
}
} while (true);
2023-05-07 22:20:31 +03:00
return readVariable;
2023-05-03 10:32:23 +03:00
}
2023-03-22 17:02:31 +02:00
2023-05-03 10:32:23 +03:00
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
2023-03-22 17:02:31 +02:00
2023-05-07 09:26:25 +03:00
final int n = protectedInput("n", input);
final int m = protectedInput("m", input);
final int a = protectedInput("a", input);
final int b = protectedInput("b", input);
2023-03-22 17:02:31 +02:00
2023-04-26 14:37:46 +03:00
input.close();
2023-05-03 10:40:04 +03:00
float s = ((float) (b + m) / 2) * (m - b + 1) * (n - a + 1);
2023-03-22 17:02:31 +02:00
System.out.println("S = " + s);
}
}