Advertisement
cyanoise

Cyanoise number game full code

Nov 25th, 2014
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.58 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class numbergame {
  5.  
  6.     public static void main(String[] args) {
  7.        
  8.         Scanner inputScanner = new Scanner(System.in);  //Open Scanner
  9.        
  10.         //Asking player name
  11.         System.out.print("Enter your name : ");
  12.         String playerName = inputScanner.nextLine();
  13.        
  14.         boolean playNumberGame = true;                  //boolean to play again
  15.         while(playNumberGame == true) {                 //repeat game system
  16.             int computerNumber = 0;
  17.            
  18.             //Ensures 0 is not generated
  19.             while (computerNumber == 0) {
  20.                 Random randomGenerator = new Random();
  21.                 computerNumber = randomGenerator.nextInt(10);
  22.             }
  23.            
  24.             //Player guess
  25.             System.out.print("Welcome " + playerName + ", please enter an integer. (1-9) : ");
  26.             int playerGuess = inputScanner.nextInt();
  27.            
  28.             //Results
  29.             System.out.println("Computer guessed the number " + computerNumber + ".");
  30.             if (playerGuess == computerNumber) {
  31.                 System.out.println("Such guess!! Much talent!! Wow!!");
  32.             } else {
  33.                 System.out.println("You got it WRONG you noob scrub. You got PWNED");
  34.             }
  35.            
  36.             //Replay feature
  37.             boolean replay = true;
  38.             while (replay == true) {
  39.                 System.out.print("Do you want to play again? (y/n) : ");
  40.                 String askReplay = inputScanner.nextLine();
  41.                
  42.                 if (askReplay.equalsIgnoreCase("y")) {
  43.                     playNumberGame = true;
  44.                     replay = false;
  45.                 } else if (askReplay.equalsIgnoreCase("n")) {
  46.                     playNumberGame = false;
  47.                     replay = false;
  48.                 } else {
  49.                     System.out.println("Unknown reply.");
  50.                 }
  51.                
  52.             }
  53.         }
  54.         inputScanner.close();   //Close Scanner
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement