Advertisement
waytoofoxy

Untitled

Mar 29th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3. public class Problem2 {
  4.     public static void main(String args[]){
  5.         Scanner input = new Scanner(System.in);
  6.         Random rand = new Random();
  7.         int number;
  8.         int guess = 0;
  9.         int counter=0;
  10.         String playagain;
  11.         number = rand.nextInt(100)+1;
  12.         while(guess!=number && guess != 101){
  13.         System.out.println("Pick a number from 1 to 100. Type 101 to quit.");
  14.             guess = input.nextInt();
  15.             if(guess>number){
  16.                 System.out.println("Your guess is high.");
  17.             }else if(guess<number){
  18.                 System.out.println("Your guess is low.");
  19.             }else if(guess == 101){
  20.                 System.out.println("Type y to play again.");
  21.                 playagain = input.nextLine();
  22.                 if(playagain=="y"){
  23.                     guess=0;
  24.                 }else{
  25.                     guess = guess;
  26.                 }
  27.                    
  28.             }else{
  29.                 System.out.println("You guessed it.");
  30.                 System.out.println("Type y to play again");
  31.                 playagain = input.nextLine();
  32.                 if(playagain == "y")
  33.                     guess = 0;
  34.                 else
  35.                     guess = guess;
  36.             }
  37.                 counter++;
  38.         }
  39.         System.out.println("Number of tries: " + counter);
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement