Assignment #65 Number-Guessing with a Counter:

Code

/// Name: Kiran O'Farrell
/// Program Name: NumberGuesserIII
/// File Name: NumberGuesserIII.java
/// Date Completed: 1/20/16

import java.util.Random;
import java.util.Scanner;

public class NumberGuesserIII {

    public static void main (String[] args) {
    
        Random r = new Random();
        Scanner keyboard = new Scanner(System.in);
        
        int secret = 1 + r.nextInt(10);
        int guess;
        int attempts = 0;
        
        System.out.println("Try to guess the secret number!");
        guess = keyboard.nextInt();
        
         while (secret != guess) {
         
         System.out.println("Sorry! You guessed wrong.");
         attempts++;
         guess = keyboard.nextInt();
         
         }
        
        if (guess == secret) {
        
            System.out.println("Great job! You guessed the right number! You got it in " + attempts + " guesses.");
            
        }
        
        else {
        
            System.out.println("Sorry! You guessed the wrong number.");
            
        }
        
    }
    
}
    

Picture of the output