diff --git a/Java/lab_1/lab_1.java b/Java/lab_1/lab_1.java index e9ce63e..d536cce 100644 --- a/Java/lab_1/lab_1.java +++ b/Java/lab_1/lab_1.java @@ -7,14 +7,12 @@ public class lab_1 { do { try { System.out.printf("Enter %s: ", variableToRead); - readVariable = input.nextInt(); - break; + return input.nextInt(); } catch (Exception e) { System.out.printf("%s must be an integer.\n", variableToRead.toUpperCase()); input.nextLine(); } } while (true); - return readVariable; } public static void main(String[] args) { @@ -27,7 +25,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); } diff --git a/Java/lab_1/lab_1.kt b/Java/lab_1/lab_1.kt new file mode 100644 index 0000000..5562024 --- /dev/null +++ b/Java/lab_1/lab_1.kt @@ -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") +} \ No newline at end of file