Jump to content

Ok so im in a class at my school that teaches java (AP Computer Science) and right now were making a program that prints a random phone number. However the thing is that the first 3 digits cannot have 8 or 9 in them and the middle 3 numbers cannot be bigger than 742. Basically i need to know how to exclude a number from something such as this:

 

num1 = generator.nextInt(random # here but cannot include 8 or 9) +1;

 

the string above needs to be able to include a 0 so it cannot just stop at 7. So basically i just need to know how to exclude certain numbers if someone could help me out i would really appreciate it.

CPUi7 4790k (4.0 Ghz)| MotherboardMSI Z97 Gaming-5 ATX LGA1150| RAM: 16 Gb Corsair Vengeance Pro(DDR3-1600, Red 4x4Gb)GPUEVGA GTX 980 4GB Superclocked ACX 2.0| CaseFractal Design Define R5 (Black with Window)| StorageWestern Digital Caviar Blue 1TB and Samsung 850 EVO 250 GB SSD| PSUCorsair HX750i 80+ Platinum| Display(s) Asus VG248QE 144Hz 24.0" CoolingCorsair H100i GTX| Keyboard: Corsair K95 RGB| MouseRazer Deathadder Chroma|

Link to comment
https://linustechtips.com/topic/450047-simple-java-number-question/
Share on other sites

Link to post
Share on other sites

Hint:

If generated number X = 8 or 9 then discard value and regenerate number X

If generated numbers X+Y+Z < 742 then accept value, or generate number within a certain range

 

EDIT: this is just one method, there are other possible solutions

Guide: DSLR or Video camera?, Guide: Film/Photo makers' useful resources, Guide: Lenses, a quick primer

Nikon D4, Nikon D800E, Fuji X-E2, Canon G16, Gopro Hero 3+, iPhone 5s. Hasselblad 500C/M, Sony PXW-FS7

ICT Consultant, Photographer, Video producer, Scuba diver and underwater explorer, Nature & humanitarian documentary producer

Link to post
Share on other sites

Math.random() * 7

This line of code will give you a random integer between 0 and 7, but you will have to figure out how to cast the outputted double value into a integer. (Not trying to be mean, but you have to learn).

 

For the second part, it is basically the same thing, but with a range of 0 - 742, so try and figure it out.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×