Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This program prompts the user to guess a lottery number until they guess correctly.
- *
- * Bomani McClendon
- * 10/1/12
- */
- import java.util.Scanner;
- public class Lottery
- {
- public static void main(String[] args)
- {
- Scanner in = new Scanner(System.in);
- //Ask to Play
- System.out.println("Do you want to play? (Y/N)");
- String agreeString = in.next();
- char agree = agreeString.charAt(0);
- //Start loop
- for(int counter=1; agree == 'Y'; counter++)
- {
- double randomNumber;
- randomNumber = Math.random();
- int lotNumbersDouble = (int)(randomNumber * 1000);
- String lotNumbersStr = String.valueOf(lotNumbersDouble);
- String.format("###.",lotNumbersStr);
- String lotNumbers = "000" + lotNumbersStr;
- int length = lotNumbers.length();
- lotNumbers = lotNumbers.substring(length - 3, length);
- //Creating Lottery Substring
- String lotStringWhole = lotNumbers;
- String lotString1 = lotStringWhole.substring(0);
- String lotString2 = lotStringWhole.substring(1);
- String lotString3 = lotStringWhole.substring(2);
- //Creating Lottery Number Pairs
- String pair1 = (lotString1 + lotString2);
- String pair2 = (lotString2 + lotString3);
- String pairBoth = (lotStringWhole);
- //Entering Guess Numbers
- System.out.println("Please enter your three numbers (e.g. 123): ");
- String lotGuess = in.next();
- //Creating Guess Substrings
- String lotGuessWhole = lotGuess;
- String lotGuessString1 = lotGuessWhole.substring(0);
- String lotGuessString2 = lotGuessWhole.substring(1);
- String lotGuessString3 = lotGuessWhole.substring(2);
- //Creating Lottery Number Pairs
- String guess1 = (lotGuessString1 + lotGuessString2);
- String guess2 = (lotGuessString2 + lotGuessString3);
- String guessWhole = (lotGuessWhole);
- if(pair1 == guess1)
- {
- System.out.println("Winner: " + lotStringWhole);
- System.out.println("Congratulations, the front pair matched.");
- }
- else
- {
- System.out.println("Winner: " + lotStringWhole);
- System.out.println("Sorry, no match. Try again next time.");
- }
- if (pair2 == guess2)
- {
- System.out.println("Winner: " + lotStringWhole);
- System.out.println("Congratulations, the end pair matched.");
- }
- else
- {
- System.out.println("Winner: " + lotStringWhole);
- System.out.println("Sorry, no match. Try again next time.");
- }
- if (pairBoth == guessWhole)
- {
- System.out.println("Winner: " + lotStringWhole);
- System.out.println("Congratulations, both pairs matched.");
- }
- else
- {
- System.out.println("Winner: " + lotStringWhole);
- System.out.println("Sorry, no match. Try again next time.");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment