-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.rs
32 lines (26 loc) · 935 Bytes
/
game.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use rand::Rng;
use std::io;
fn main() {
println!("Welcome to the Guessing Game!");
let secret_number = rand::thread_rng().gen_range(1..=100);
loop {
println!("Please enter your guess:");
let mut guess = String::new();
io::stdin().read_line(&mut guess).expect("Failed to read line");
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => {
println!("Invalid input. Please enter a number.");
continue;
}
};
match guess.cmp(&secret_number) {
std::cmp::Ordering::Less => println!("Too low! Try again."),
std::cmp::Ordering::Greater => println!("Too high! Try again."),
std::cmp::Ordering::Equal => {
println!("Congratulations! You guessed the correct number: {}", secret_number);
break;
}
}
}
}