Added a Kotlin variation.
This commit is contained in:
parent
2494bea36c
commit
a97f742be2
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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