OOP_IO-2x_2023-mirror/Java/Lab 1/Lab_1.java

35 lines
1002 B
Java
Raw Normal View History

2023-03-22 17:02:31 +02:00
import java.util.Scanner;
public class Lab_1 {
2023-05-03 10:32:23 +03:00
public static int protectedInput(String variable_to_read, Scanner input) {
int read_variable;
2023-05-02 09:58:31 +03:00
do {
try {
2023-05-03 10:32:23 +03:00
System.out.printf("Enter %s: ", variable_to_read);
read_variable = input.nextInt();
2023-05-02 09:58:31 +03:00
break;
} catch (Exception e) {
2023-05-03 10:32:23 +03:00
System.out.printf("%s must be an integer.\n", variable_to_read.toUpperCase());
2023-05-02 09:58:31 +03:00
input.nextLine();
}
} while (true);
2023-05-03 10:32:23 +03:00
return read_variable;
}
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);
}
}