Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- public class Problem2 {
- public static void main(String args[]){
- Scanner input = new Scanner(System.in);
- Random rand = new Random();
- int number;
- int guess = 0;
- int counter=0;
- String playagain;
- number = rand.nextInt(100)+1;
- while(guess!=number && guess != 101){
- System.out.println("Pick a number from 1 to 100. Type 101 to quit.");
- guess = input.nextInt();
- if(guess>number){
- System.out.println("Your guess is high.");
- }else if(guess<number){
- System.out.println("Your guess is low.");
- }else if(guess == 101){
- System.out.println("Type y to play again.");
- playagain = input.nextLine();
- }else{
- System.out.println("You guessed it.");
- System.out.println("Type y to play again");
- }
- counter++;
- }
- System.out.println("Number of tries: " + counter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement