diff --git a/Java/Lab 1/Lab_1.java b/Java/Lab 1/Lab_1.java index c3af60d..150db96 100644 --- a/Java/Lab 1/Lab_1.java +++ b/Java/Lab 1/Lab_1.java @@ -18,17 +18,12 @@ public class Lab_1 { } public static void main(String[] args) { - int n, m, a, b; - Scanner input = new Scanner(System.in); - n = protectedInput("n", input); - - m = protectedInput("m", input); - - a = protectedInput("a", input); - - b = protectedInput("b", input); + final int n = protectedInput("n", input); + final int m = protectedInput("m", input); + final int a = protectedInput("a", input); + final int b = protectedInput("b", input); input.close(); diff --git a/Rust/lab_1/src/main.rs b/Rust/lab_1/src/main.rs index c166139..295ec18 100644 --- a/Rust/lab_1/src/main.rs +++ b/Rust/lab_1/src/main.rs @@ -1,20 +1,22 @@ -fn main() { - // use text_io::scan; - use text_io::read; - - print!("Enter m: "); - let m: f32 = read!(); - - print!("Enter n: "); - let n: f32 = read!(); - - print!("Enter a: "); - let a: f32 = read!(); - - print!("Enter b: "); - let b: f32 = read!(); - - let s: f32 = ((b + m) / 2.0) * (m - b + 1.0) * (n - a + 1.0); - - println!("S= {}", s); +fn protected_read(variable_name: &str) -> i32 { + use text_io::try_read; + loop { + print!("Enter {}: ", variable_name); + let read_result: Result = try_read!(); + match read_result { + Ok(read_integer) => return read_integer, + Err(_e) => println!("{} must be an integer!", variable_name.to_uppercase()), + } + } +} + +fn main() { + let n: i32 = protected_read("n"); + let m: i32 = protected_read("m"); + let a: i32 = protected_read("a"); + let b: i32 = protected_read("b"); + + let s: f32 = ((b + m) as f32 / 2f32) * ((m - b + 1) * (n - a + 1)) as f32; + + println!("S = {}", s); }