Advertisement
KeeganT

Ch7Ex10

Mar 19th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.80 KB | None | 0 0
  1. package ch7ex10;
  2. import java.util.Scanner;
  3. import java.util.Random;
  4.  
  5. public class Ch7Ex10
  6. {
  7.     public static void hint(int num, int rand)
  8.     {
  9.         if(num>rand)System.out.println("Hint: Try a lower number.");
  10.         if(num<rand)System.out.println("Hint: Try a higher number.");
  11.     }
  12.     public static void main(String[] args)
  13.     {
  14.         Scanner sc=new Scanner(System.in);
  15.         Random gen=new Random();
  16.         int rand=gen.nextInt(20)+1;
  17.         int num;
  18.         do
  19.         {
  20.             System.out.print("Enter a number between 1 and 20: ");
  21.             num=sc.nextInt();
  22.             hint(num,rand);
  23.         }while(num!=rand);
  24.         System.out.println("Comptuers number: "+rand);
  25.         System.out.println("Players number: "+num);
  26.         System.out.println("You won!");
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement