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 39 40 41 42 43 44 45 46 47 48
| use rand::Rng; use std::cmp::Ordering; use std::io;
fn main() { println!("猜数游戏!"); let secret_number = rand::thread_rng().gen_range(1, 101);
loop { println!("猜测一个数"); let mut guess = String::new(); io::stdin().read_line(&mut guess).expect("无法读取行");
let guess: u32 = match guess.trim().parse() { Ok(num) => num, Err(_) => { println!("请输入一个数!"); continue; }, }; println!("你猜的数是:{}", guess);
match guess.cmp(&secret_number) { Ordering::Less => println!("太小了!"), Ordering::Greater => println!("太大了!"), Ordering::Equal => { println!("你赢了!"); break; } } }
}
|