Skip to content

Commit

Permalink
数当てゲームの完成形 #4
Browse files Browse the repository at this point in the history
  • Loading branch information
euledge committed Feb 10, 2019
1 parent e0c3071 commit 216d246
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions Chapter2/gussing_game/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
extern crate rand;

use std::io;
use std::cmp::Ordering;
use rand::Rng;
use std::cmp::Ordering;
use std::io;

fn main() {
println!("Guess the number!");

let secret_number = rand::thread_rng().gen_range(1, 101);
println!("The secret number is: {}", secret_number);

println!("Please input your guess.");
loop {
println!("Please input your guess.");

let mut guess = String::new();
let mut guess = String::new();

io::stdin()
.read_line(&mut guess)
.expect("failed to read line");
io::stdin()
.read_line(&mut guess)
.expect("failed to read line");

let guess:i32 = guess.trim().parse().expect("Please type a number!");
let guess: i32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};

println!("You guessed: {}", guess);
println!("You guessed: {}", guess);

match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too Big!"),
Ordering::Equal => println!("You Win!")
match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too Big!"),
Ordering::Equal => {
println!("You Win!");
break;
}
}
}
}

0 comments on commit 216d246

Please sign in to comment.