Created
January 13, 2022 04:10
-
-
Save topherPedersen/1607ea34fb2a74d30b4e98bc830ca552 to your computer and use it in GitHub Desktop.
Magic 8 Ball in Rust (Small Example for Learning Rust)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::io; | |
| use rand::Rng; | |
| fn main() { | |
| println!("Welcome to Magic 8-Ball"); | |
| loop { | |
| println!("What is your question?"); | |
| let mut question = String::new(); | |
| let answers = [ | |
| "It is certain.", | |
| "Reply hazy, try again.", | |
| "My sources say no." | |
| ]; | |
| let random_number = rand::thread_rng().gen_range(0..3); | |
| io::stdin() | |
| .read_line(&mut question) | |
| .expect("ERROR: PC LOAD LETTER"); | |
| println!("{}", answers[random_number]); | |
| let mut play_again = String::new(); | |
| println!("Would you like to play again (yes or no)?"); | |
| io::stdin() | |
| .read_line(&mut play_again) | |
| .expect("ERROR: PC LOAD LETTER"); | |
| let mut play_again = play_again.trim(); | |
| if play_again.eq("y") || play_again.eq("Y") || play_again.eq("yes") || play_again.eq("YES") || play_again.eq("Yes") { | |
| continue; | |
| } else { | |
| println!("Goodbye"); | |
| break; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment