Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Guesses the user's number on a high low basis
- *
- * @author Ulizio
- * @version 3.0
- */
- import java.util.Scanner;
- public class GuessingGameUlizio
- {
- public static void main(String [] args) {
- System.out.println("I am Earl. I will be guessing your number today.");
- int max = getMax();
- int min = getMin();
- int roundCount = 0;
- while(true){
- int currentGuess = guess(max, min);
- System.out.println(currentGuess);
- boolean finished = check(currentGuess);
- if(finished == false) {
- char highOrLow = highLow();
- if(highOrLow == 'h') {
- max = (max + min)/2;
- }
- else if(highOrLow == 'l') {
- min = (max + min)/2;
- }
- else {
- }
- }
- if(finished == true) {
- System.out.println("I guessed your number in " + roundCount + " rounds!");
- break;
- }
- else {
- }
- roundCount++;
- }
- }
- public static int guess(int max, int min) {
- /**
- * This method calculates Earl's guess.
- */
- int sumMaxMin = max + min;
- int guess = sumMaxMin/2;
- return(guess);
- }
- public static char highLow() {
- /**
- * This method asks the user if the guess was high or low.
- */
- Scanner scan = new Scanner(System.in);
- System.out.println("If my guess was too high, type h. If it was too low, type l.");
- char userInput = scan.next().charAt(0);
- return(userInput);
- }
- public static int getMax() {
- /**
- * This method gets the user's max.
- */
- Scanner scan = new Scanner(System.in);
- System.out.println("High End?");
- int max = scan.nextInt();
- return(max);
- }
- public static int getMin() {
- /**
- * This method gets the user's min.
- */
- Scanner scan = new Scanner(System.in);
- System.out.println("Low End?");
- int min = scan.nextInt();
- return(min);
- }
- public static boolean check(int guess) {
- /**
- * This method checks the computer's guess.
- */
- Scanner scan = new Scanner(System.in);
- System.out.println("Was I correct? Type y for yes or n for no.");
- char userInput = scan.next().charAt(0);
- boolean finished = false;
- if(userInput == 'y') {
- finished = true;
- }
- else if(userInput == 'n') {
- finished = false;
- }
- else {
- }
- return(finished);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement