Compare commits
5 Commits
ca2b85ef9e
...
a8cbadbe7d
Author | SHA1 | Date |
---|---|---|
Rhinemann | a8cbadbe7d | |
Rhinemann | 64c9e43742 | |
Rhinemann | 5ca9ab6c17 | |
Rhinemann | a97f742be2 | |
Rhinemann | 2494bea36c |
|
@ -2,19 +2,16 @@ import java.util.Scanner;
|
|||
|
||||
public class lab_1 {
|
||||
|
||||
public static int protectedInput(String variable_to_read, Scanner input) {
|
||||
int read_variable;
|
||||
public static int protectedInput(String variableToRead, Scanner input) {
|
||||
do {
|
||||
try {
|
||||
System.out.printf("Enter %s: ", variable_to_read);
|
||||
read_variable = input.nextInt();
|
||||
break;
|
||||
System.out.printf("Enter %s: ", variableToRead);
|
||||
return input.nextInt();
|
||||
} catch (Exception e) {
|
||||
System.out.printf("%s must be an integer.\n", variable_to_read.toUpperCase());
|
||||
System.out.printf("%s must be an integer.\n", variableToRead.toUpperCase());
|
||||
input.nextLine();
|
||||
}
|
||||
} while (true);
|
||||
return read_variable;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
@ -27,7 +24,7 @@ public class lab_1 {
|
|||
|
||||
input.close();
|
||||
|
||||
float s = ((float) (b + m) / 2) * (m - b + 1) * (n - a + 1);
|
||||
final float s = ((float) (b + m) / 2) * (m - b + 1) * (n - a + 1);
|
||||
|
||||
System.out.println("S = " + s);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
fun protectedInput(variableName: String): Int {
|
||||
do {
|
||||
try {
|
||||
print("Enter $variableName: ")
|
||||
return readln().toInt()
|
||||
} catch (e: Exception) {
|
||||
println("${variableName.uppercase()} must be an integer!")
|
||||
}
|
||||
} while (true)
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val n: Int = protectedInput("n")
|
||||
val m: Int = protectedInput("m")
|
||||
val a: Int = protectedInput("a")
|
||||
val b: Int = protectedInput("b")
|
||||
|
||||
val s: Float = (b + m).toFloat() / 2 * (m - b + 1) * (n - a + 1)
|
||||
|
||||
println("S = $s")
|
||||
}
|
Loading…
Reference in New Issue