KeeganT

Ch7Ex8

Mar 19th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.51 KB | None | 0 0
  1. package ch7ex8;
  2. import java.util.Scanner;
  3. import java.util.Random;
  4.  
  5. public class Ch7Ex8
  6. {
  7.     public static void main(String[] args)
  8.     {
  9.         Scanner sc=new Scanner(System.in);
  10.         Random generator = new Random();
  11.         int rand, score=1000, guessCount=0;
  12.         System.out.println("High Low Game\n\nRules:\nNumbers 1 to 6 are low.\nNumbers 8 to 13 are high.");
  13.         System.out.println("Number 7 is neither high nor low.\n\nYou start with 1000 points.\n");
  14.         for(int c=0;score>0;c++)
  15.         {
  16.             guessCount++;
  17.             System.out.println("Current Score: "+score);
  18.             System.out.print("Enter points to risk: ");
  19.             int risk=sc.nextInt();
  20.             while(risk>score)
  21.             {
  22.                 System.out.println("Insufficient points!\nEnter a new risk: ");
  23.                 risk=sc.nextInt();  
  24.             }
  25.             rand=generator.nextInt(13)+1;
  26.             System.out.print("Predict High (1) or Low (0): ");
  27.             int guess=sc.nextInt();
  28.             System.out.println("The number was: "+rand);
  29.             if((guess==0&&rand<7)||(guess==1&&rand>7))
  30.             {
  31.                 score+=risk;
  32.                 System.out.println("You gain "+risk+" points!");
  33.             }
  34.             else
  35.             {
  36.                 score-=risk;
  37.                 System.out.println("You lose "+risk+" points!");
  38.             }
  39.         }
  40.         System.out.println("Your score has reached 0!\nTotal guesses: "+guessCount+"\nThanks for playing!");
  41.     }  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment