I am new to programming in general, so I try to be as specific as possible in this matter. There is this book that I am doing some exercises. I managed to do more than half of what they say, but this is just one piece of material that I struggled to find out.
I will write a question and after that my code,
βWrite down an application that creates and prints a random phone number of the form XXX-XXX-XXXX. Include a dash on the output. Do not let the first three digits contain 8 or 9 (but no more restrictive), and make sure that the second set of three digits is no more 742. Hint. Consider the easiest way to build a phone number. Each number does not have to be determined separately. "
OK, the highlighted sentence is what I'm looking at. Here is my code:
import java.util.Random; public class PP33 { public static void main (String[] args) { Random rand = new Random(); int num1, num2, num3; num1 = rand.nextInt (900) + 100; num2 = rand.nextInt (643) + 100; num3 = rand.nextInt (9000) + 1000; System.out.println(num1+"-"+num2+"-"+num3); } }
How can i do this? I am in Chapter 3, so we have not discussed if there exist etc. But aliases, String class, packages, import declaration, random class, math class, formatting output (decimal- and numberFormat), Printf, Enumeration classes and Wrapper + autoboxing, so consider a question based only on these assumptions, please.
There are no errors in the code.
Thanks!
source share