Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Write an application that creates and prints a random phone number of the form XXX - XXX - XXXX.
- * Include the dashes in the output. Do not let the first three digits contain an 8 or 9,
- * make sure that the second set of three digits is not greater than 742.
- */
- public class Main {
- public static int generateRandomInteger(int max) {
- return (int)(java.lang.Math.random() * (max+1));
- }
- public static int generateRandomInteger(int min, int max) {
- return (int) (java.lang.Math.random() * (max+1) / min);
- }
- public static void main(java.lang.String[] args) {
- System.out.println("Generating telephone number...");
- int[] areaCode = new int[3];
- int[] firstDigits = new int[3];
- int[] lastDigits = new int[4];
- String outcome = "";
- //Fill the area code
- for(int i = 0; i < areaCode.length; i++) {
- areaCode[i] = generateRandomInteger(7); //Will allow any number between 0 - 7
- outcome += ""+areaCode[i];
- }
- outcome += " - ";
- //Fill the first digits.
- for(int i2 = 0; i2 < firstDigits.length; i2++) {
- switch(i2) {
- case 0:
- firstDigits[i2] = generateRandomInteger(2, 7); // Will allow any digits between 2-7
- break;
- case 1:
- firstDigits[i2] = generateRandomInteger(4); /// Will allow any digits between 0-4
- break;
- case 2:
- firstDigits[i2] = generateRandomInteger(2); // Will allow any digits between 0-2;
- break;
- }
- outcome += ""+firstDigits[i2];
- }
- outcome += " - ";
- //Fill the last
- for(int i3 = 0; i3 < lastDigits.length; i3++) {
- lastDigits[i3] = generateRandomInteger(9); // Allows any digits between 0-9
- outcome += ""+lastDigits[i3];
- }
- //Print it
- System.out.println(outcome);
- }
- }
Add Comment
Please, Sign In to add comment