Compare commits

...

5 Commits

Author SHA1 Message Date
Rhinemann a8cbadbe7d Added a Kotlin variation. 2023-05-07 23:15:50 +03:00
Rhinemann 64c9e43742 Added a Kotlin variation. 2023-05-07 23:15:28 +03:00
Rhinemann 5ca9ab6c17 Added a Kotlin variation. 2023-05-07 23:14:50 +03:00
Rhinemann a97f742be2 Added a Kotlin variation. 2023-05-07 23:14:24 +03:00
Rhinemann 2494bea36c Camel case. 2023-05-07 22:20:31 +03:00
2 changed files with 26 additions and 8 deletions

View File

@ -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);
}

21
Java/lab_1/lab_1.kt Normal file
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")
}