/** * The computer picks heads (true) or tails (false). The user guesses. */ import java.util.Scanner; public class MindReaderWhileLoop2 { public static void main(String[] args) { String computerChoice; // true means heads String userChoice; // true means heads Scanner in = new Scanner(System.in); int computerScore = 0; int userScore = 0; while(true) { // Prompt user for their guess System.out.println("What do you believe the computer chose?: h=heads, t=tails, q=quit): "); String userInput = in.next(); String first = userInput.substring(0,1); if (first.equalsIgnoreCase("h") ) userChoice = "heads"; else if (first.equalsIgnoreCase("t")) userChoice = "tails"; else { userChoice = "quit" ; System.out.println("Good-bye"); break; } // Computer picks choice. if (Math.random() < .5) computerChoice = "heads"; else computerChoice = "tails"; System.out.print("The computer chose " + computerChoice); System.out.println(" and you guessed " + userChoice + "."); // Check who won. if ( computerChoice.equals(userChoice) ) { System.out.println("You are correct - CONGRATULATIONS!"); userScore++; } else { System.out.println("Sorry, you are wrong. Better luck next time."); computerScore++; } System.out.println("User Score: " + userScore + "\t\tComputer Score: " + computerScore); } } }