SHARE
TWEET

Untitled

a guest Jan 18th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  /**
  2.  * This program prompts the user to guess a lottery number until they guess correctly.
  3.  *
  4.  * Bomani McClendon
  5.  * 10/1/12
  6.  */
  7. import java.util.Scanner;
  8. public class Lottery
  9. {
  10.     public static void main(String[] args)
  11.     {
  12.        
  13.         Scanner in = new Scanner(System.in);
  14.        
  15.         //Ask to Play
  16.         System.out.println("Do you want to play? (Y/N)");
  17.         String agreeString = in.next();
  18.         char agree = agreeString.charAt(0);
  19.        
  20.     //Start loop
  21.         for(int counter=1; agree == 'Y'; counter++)
  22.         {
  23.      
  24.         double randomNumber;
  25.         randomNumber = Math.random();
  26.         int lotNumbersDouble = (int)(randomNumber * 1000);
  27.         String lotNumbersStr = String.valueOf(lotNumbersDouble);
  28.         String.format("###.",lotNumbersStr);
  29.         String lotNumbers = "000" + lotNumbersStr;
  30.         int length = lotNumbers.length();
  31.         lotNumbers = lotNumbers.substring(length - 3, length);
  32.        
  33.         //Creating Lottery Substring
  34.         String lotStringWhole = lotNumbers;
  35.         String lotString1 = lotStringWhole.substring(0);
  36.         String lotString2 = lotStringWhole.substring(1);
  37.         String lotString3 = lotStringWhole.substring(2);
  38.        
  39.         //Creating Lottery Number Pairs
  40.         String pair1 = (lotString1 + lotString2);
  41.         String pair2 = (lotString2 + lotString3);
  42.         String pairBoth = (lotStringWhole);
  43.        
  44.         //Entering Guess Numbers
  45.         System.out.println("Please enter your three numbers (e.g. 123): ");
  46.         String lotGuess = in.next();
  47.        
  48.         //Creating Guess Substrings
  49.         String lotGuessWhole = lotGuess;
  50.         String lotGuessString1 = lotGuessWhole.substring(0);
  51.         String lotGuessString2 = lotGuessWhole.substring(1);
  52.         String lotGuessString3 = lotGuessWhole.substring(2);
  53.        
  54.         //Creating Lottery Number Pairs
  55.         String guess1 = (lotGuessString1 + lotGuessString2);
  56.         String guess2 = (lotGuessString2 + lotGuessString3);
  57.         String guessWhole = (lotGuessWhole);
  58.        
  59.  
  60.         if(pair1 == guess1)
  61.             {
  62.             System.out.println("Winner: " + lotStringWhole);
  63.             System.out.println("Congratulations, the front pair matched.");
  64.             }
  65.         else  
  66.             {
  67.             System.out.println("Winner: " + lotStringWhole);
  68.             System.out.println("Sorry, no match. Try again next time.");
  69.             }
  70.          if (pair2 == guess2)
  71.             {
  72.             System.out.println("Winner: " + lotStringWhole);
  73.             System.out.println("Congratulations, the end pair matched.");
  74.             }
  75.         else
  76.             {
  77.             System.out.println("Winner: " + lotStringWhole);
  78.             System.out.println("Sorry, no match. Try again next time.");
  79.             }
  80.         if (pairBoth == guessWhole)
  81.             {
  82.             System.out.println("Winner: " + lotStringWhole);
  83.             System.out.println("Congratulations, both pairs matched.");
  84.             }
  85.         else
  86.              {
  87.             System.out.println("Winner: " + lotStringWhole);
  88.             System.out.println("Sorry, no match. Try again next time.");
  89.             }
  90.     }
  91.   }
  92.  
  93. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top