Added a Kotlin variation.

This commit is contained in:
2023-05-07 23:14:24 +03:00
parent 2494bea36c
commit a97f742be2
2 changed files with 23 additions and 4 deletions
+2 -4
View File
@@ -7,14 +7,12 @@ public class lab_1 {
do { do {
try { try {
System.out.printf("Enter %s: ", variableToRead); System.out.printf("Enter %s: ", variableToRead);
readVariable = input.nextInt(); return input.nextInt();
break;
} catch (Exception e) { } catch (Exception e) {
System.out.printf("%s must be an integer.\n", variableToRead.toUpperCase()); System.out.printf("%s must be an integer.\n", variableToRead.toUpperCase());
input.nextLine(); input.nextLine();
} }
} while (true); } while (true);
return readVariable;
} }
public static void main(String[] args) { public static void main(String[] args) {
@@ -27,7 +25,7 @@ public class lab_1 {
input.close(); 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); System.out.println("S = " + s);
} }
+21
View File
@@ -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")
}