123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- import java.util.Scanner;
- import java.util.Random;
- /**
- * Write a description of class TooLargeTooSmall here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- public class TooLargeTooSmall
- {
- Random rand = new Random();
- private int secretNumber = rand.nextInt(100)+1;
- // instance variables - replace the example below with your own
-
- /**
- * Constructor for objects of class TooLargeTooSmall
- */
- public TooLargeTooSmall()
- {
- // initialise instance variables
- int guess = 0;
- int count = 0;
- Scanner scan = new Scanner(System.in);
- System.out.println("Guess a number between 0 to 100");
-
- for (int i=0; i<=100; i++)
- {
- guess = scan.nextInt();
- if (guess>secretNumber)
- {
- System.out.println("Too large");
- count++;
- }
- else if (guess < secretNumber)
- {
- System.out.println("Too small");
- count++;
- }
- else
- {System.out.println("You are correct! \nNumber of guesses: " + (count+1));
- break;
- }
-
- }
- }
-
- }
|