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 33 34 35 36 37 38
|
use rand::Rng; use std::cmp::Ordering; use std::io::{self, Write};
fn main() { println!("=== 猜字游戏开始 ===");
let secret_number = rand::thread_rng().gen_range(1..101);
loop { print!("请输入一个0~100之间的整数: "); io::stdout().flush().unwrap();
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(_) => continue, };
match guess.cmp(&secret_number) { Ordering::Less => println!("~~~~您输入的数字偏小了~~~~"), Ordering::Greater => println!("~~~~您输入的数字偏大了~~~~~"), Ordering::Equal => { println!("^_^恭喜您猜对了^_^"); break; } } } }
|