looping
This commit is contained in:
parent
53a2181a80
commit
76e0c04081
@ -1,21 +1,33 @@
|
|||||||
use std::io;
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
use std::cmp::Ordering;
|
||||||
|
use std::io;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Guess the number!");
|
println!("Guess the number!");
|
||||||
|
|
||||||
let secret_number = rand::thread_rng().gen_range(1..=100);
|
let secret_number = rand::thread_rng().gen_range(1..=100);
|
||||||
|
|
||||||
println!("psst. the secret number is {secret_number}");
|
println!("psst. the secret number is {secret_number}");
|
||||||
|
loop {
|
||||||
|
println!("Please input your guess.");
|
||||||
|
let mut guess = String::new();
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut guess)
|
||||||
|
.expect("Failed to read line");
|
||||||
|
|
||||||
println!("Please input your guess.");
|
let guess: u32 = match guess.trim().parse() {
|
||||||
|
Ok(num) => num,
|
||||||
|
Err(_) => continue,
|
||||||
|
};
|
||||||
|
|
||||||
let mut guess = String::new();
|
|
||||||
|
|
||||||
io::stdin()
|
println!("You guessed {guess}");
|
||||||
.read_line(&mut guess)
|
|
||||||
.expect("Failed to read line");
|
|
||||||
|
|
||||||
println!("You guessed {guess}");
|
|
||||||
|
|
||||||
|
match guess.cmp(&secret_number) {
|
||||||
|
Ordering::Less => println!("Too Small"),
|
||||||
|
Ordering::Greater => println!("too big"),
|
||||||
|
Ordering::Equal => {
|
||||||
|
println!("you win");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user