Advertisement
SourCB

Programming game

May 16th, 2014
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class ch13
  5. {
  6.   public static void main (String[] args )
  7.   {
  8.     Scanner scan = new Scanner( System.in );
  9.  
  10.     Random rand = new Random();
  11.     int r, g, x = 1;
  12.  
  13.      r = (rand.nextInt(10)+1);
  14.  
  15.      System.out.println("I am thinking of a number from 1 to 10");
  16.      System.out.println(" You must guess what it is in 3 tries");
  17.      System.out.println("Enter a guess: ");
  18.      g = scan.nextInt();
  19.  
  20.  
  21.      while ( x < 3 )
  22.      {
  23.          x = x + 1;
  24.  
  25.      if ( g <= r - 3 || g >= r + 3 )
  26.      {
  27.          System.out.println("Cold");
  28.      }
  29.  
  30.      else if ( g == r - 2 || g == r + 2 )
  31.      {
  32.          System.out.println("Warm");
  33.      }
  34.  
  35.      else if ( g == r - 1 || g == r + 1 )
  36.      {
  37.          System.out.println("Hot");
  38.      }
  39.  
  40.      if ( g == r )
  41.      {
  42.          System.out.println("RIGHT!");
  43.          System.out.println("You have won the game!");
  44.      }
  45.      
  46.  
  47.      else
  48.      {
  49.          System.out.println("Wrong! Guess again!");
  50.          g = scan.nextInt();
  51.      }
  52.  
  53.      if ( x == 3 && g != r )
  54.          {
  55.              System.out.println("The correct number was " + r);
  56.              System.out.println("You have lost the game. You are now a loser");
  57.          }
  58.  
  59.      else if ( x == 3 && g == r )
  60.          {
  61.              System.out.println("RIGHT!");
  62.              System.out.println("You have won the game!");
  63.          }
  64.  
  65.  
  66.       }
  67.      
  68.  
  69.  
  70.  
  71.  
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement