Advertisement
andrewpr52

NumberGame (1) - NumberGame class (main)

Oct 20th, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.06 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class NumberGame
  4. {
  5.     public static void main (String[] args)
  6.     {
  7.         //Initial print statement. Prompts the user for his/her name
  8.         System.out.println("Hello! Please begin by entering your name: ");
  9.        
  10.         //Creates a Scanner to allow the user to enter their name
  11.         Scanner scan1 = new Scanner (System.in);
  12.         String name = scan1.nextLine();
  13.        
  14.         //Explains the game to the user. Instructions are broken up into smaller sections.
  15.         System.out.println("Thanks " + name + "! Let's  begin by explaining the game.");
  16.         System.out.println();
  17.         System.out.println("The object of the game is to correctly guess the secret number.");
  18.         System.out.println("The number is randomly generated, and is different every time you play.");
  19.         System.out.println();
  20.         System.out.println("***Press ENTER to Continue***");
  21.         scan1.nextLine();
  22.         System.out.println("There are three levels, each harder than the last.");
  23.         System.out.println("In level one, you have three chances to correctly guess the number. The number will be anywhere from 1 to 10.");
  24.         System.out.println("In level two, you have five chances to correctly guess the number. The number will be anywhere from 1 to 50.");
  25.         System.out.println("In level three, you have seven chances to correctly guess the number. The number will be anywhere from 1 to 100.");
  26.         System.out.println();
  27.         System.out.println("***Press ENTER to Continue***");
  28.         scan1.nextLine();
  29.         System.out.println("To guess a number, type the number in and press enter.");
  30.         System.out.println("You will be told whether or not you correctly guessed the number.");
  31.         System.out.println("If you guess incorrectly, you will be given a hint as to whether the number is higher or lower than what you guessed.");
  32.         System.out.println("If you run out of chances to correctly guess the number, then you lose.");
  33.         System.out.println();
  34.         System.out.println("***Press ENTER to Start Game***");
  35.         scan1.nextLine();
  36.         System.out.println("Please begin by selecting a level:");
  37.         System.out.println();
  38.         System.out.println("Level One - numbers 1-10 (type \"1\" to play)");
  39.         System.out.println("Level Two - numbers 1-50 (type \"2\" to play)");
  40.         System.out.println("Level Three - numbers 1-100 (type \"3\" to play)");
  41.         System.out.println();
  42.         System.out.println("Type \"exit\" to end the game.");
  43.        
  44.         //Allows the program to continue running if the user selects an invalid number or string during the level selection.
  45.         while (true)
  46.         {
  47.             doMethod();
  48.         }
  49.     }
  50.    
  51.     public static void doMethod()
  52. {      
  53.         //Creates a second Scanner to allow the user to select the level they wish to play
  54.         Scanner scan2 = new Scanner(System.in);
  55.         String level = scan2.nextLine();
  56.        
  57.         //Level One
  58.     {  
  59.         if (level.equals("1"))
  60.         {
  61.             System.out.println("You chose Level 1. You have 3 chances to guess the number.");
  62.            
  63.             int num = LevelOne.randInt();
  64.             int guess;
  65.             int chances=3;
  66.             boolean win = false;
  67.            
  68.             while (win == false)
  69.             {
  70.                 if (chances == 0)
  71.                 {
  72.                     System.out.println();
  73.                     System.out.println("You have no chances left! You lose!");
  74.                     System.out.println("The number was " + num + ".");
  75.                     System.out.println();
  76.                     System.out.println("Thanks for playing!");
  77.                
  78.                     System.exit(0);
  79.                 }
  80.                
  81.                 else
  82.                 {  
  83.                 System.out.println();
  84.                 System.out.println("Guess a number from 1 to 10.");
  85.                 guess = scan2.nextInt();
  86.                 chances--;
  87.                
  88.                     if (guess < num)
  89.                     {
  90.                         System.out.println("Your guess was too low.");
  91.                         System.out.println();
  92.                         System.out.println("Attempts remaining: " + chances);
  93.                     }  
  94.                     else if (guess > num)
  95.                     {
  96.                         System.out.println("Your guess was too high.");
  97.                         System.out.println();
  98.                         System.out.println("Attempts remaining: " + chances);
  99.                     }
  100.                     else if (guess == num)
  101.                     {
  102.                         win = true;
  103.                     }
  104.                 }
  105.             }
  106.             System.out.println("You win! Congratulations!");
  107.             System.out.println("The number was " + num + ".");
  108.             System.out.println();
  109.             System.out.println("Thanks for playing!");
  110.            
  111.             System.exit(0);
  112.         }  
  113.        
  114.         //Level Two
  115.         else if (level.equals("2"))
  116.         {
  117.             System.out.println("You chose Level 2. You have 5 chances to guess the number.");
  118.            
  119.             int num = LevelTwo.randInt();
  120.             int guess;
  121.             int chances=5;
  122.             boolean win = false;
  123.            
  124.             while (win == false)
  125.             {
  126.                 if (chances == 0)
  127.                 {
  128.                     System.out.println();
  129.                     System.out.println("You have no chances left! You lose!");
  130.                     System.out.println("The number was " + num + ".");
  131.                     System.out.println();
  132.                     System.out.println("Thanks for playing!");
  133.                
  134.                     System.exit(0);
  135.                 }
  136.                
  137.                 else
  138.                 {  
  139.                 System.out.println();
  140.                 System.out.println("Guess a number from 1 to 50.");
  141.                 guess = scan2.nextInt();
  142.                 chances--;
  143.                    
  144.                     if (guess < num)
  145.                     {
  146.                         System.out.println("Your guess was too low.");
  147.                         System.out.println();
  148.                         System.out.println("Attempts remaining: " + chances);
  149.                     }  
  150.                     else if (guess > num)
  151.                     {
  152.                         System.out.println("Your guess was too high.");
  153.                         System.out.println();
  154.                         System.out.println("Attempts remaining: " + chances);
  155.                     }
  156.                     else if (guess == num)
  157.                     {
  158.                         win = true;
  159.                     }
  160.                 }
  161.             }
  162.             System.out.println("You win! Congratulations!");
  163.             System.out.println("The number was " + num + ".");
  164.             System.out.println();
  165.             System.out.println("Thanks for playing!");
  166.            
  167.             System.exit(0);
  168.         }
  169.        
  170.         //Level Three
  171.         else if (level.equals("3"))
  172.         {
  173.             System.out.println("You chose Level 3. You have 7 chances to guess the number.");
  174.            
  175.             int num = LevelTwo.randInt();
  176.             int guess;
  177.             int chances=7;
  178.             boolean win = false;
  179.            
  180.             while (win == false)
  181.             {
  182.                 if (chances == 0)
  183.                 {
  184.                     System.out.println();
  185.                     System.out.println("You have no chances left! You lose!");
  186.                     System.out.println("The number was " + num + ".");
  187.                     System.out.println();
  188.                     System.out.println("Thanks for playing!");
  189.                
  190.                     System.exit(0);
  191.                 }
  192.                
  193.                 else
  194.                 {  
  195.                 System.out.println();
  196.                 System.out.println("Guess a number from 1 to 100.");
  197.                 guess = scan2.nextInt();
  198.                 chances--;
  199.                    
  200.                     if (guess < num)
  201.                     {
  202.                         System.out.println("Your guess was too low.");
  203.                         System.out.println();
  204.                         System.out.println("Attempts remaining: " + chances);
  205.                     }  
  206.                     else if (guess > num)
  207.                     {
  208.                         System.out.println("Your guess was too high.");
  209.                         System.out.println();
  210.                         System.out.println("Attempts remaining: " + chances);
  211.                     }
  212.                     else if (guess == num)
  213.                     {
  214.                         win = true;
  215.                     }
  216.                 }
  217.             }
  218.             System.out.println("You win! Congratulations!");
  219.             System.out.println("The number was " + num + ".");
  220.             System.out.println();
  221.             System.out.println("Thanks for playing!");
  222.            
  223.             System.exit(0);
  224.         }
  225.        
  226.         //Allows the user to exit the program if they wish
  227.         else if (level.equals("exit"))
  228.         {
  229.             System.exit(0);
  230.         }
  231.        
  232.         //Allows the user to enter another input if their first one was invalid
  233.         else
  234.             System.out.println("Please select 1, 2, or 3, or type \"exit\" to end the game.");
  235.            
  236.     }
  237.    
  238. }
  239.    
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement