I'm new to programming in general so I'm trying to be as specific as possible in this question. There's this book that I'm doing some exercises on. I managed to do more than half of what they say, but it's just one input that I have been struggling to find out.
I'll write the question and thereafter my code,
"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 (but don't be more restrictive than that), and make sure that the second set of three digits is not greater than 742. Hint: Think through the easiest way to construct the phone number. Each digit does not have to be determined separately."
OK, the highlighted sentence is what I'm looking at. Here's 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 am I suppose to do this? I'm on chapter 3 so we have not yet discussed if statements etcetera, but Aliases, String class, Packages, Import declaration, Random Class, Math Class, Formatting output (decimal- & numberFormat), Printf, Enumeration & Wrapper classes + autoboxing. So consider answer the question based only on these assumptions, please.
The code doesn't have any errors.
Thank you!
Seeing as this appears to be homework I feel like an explanation of what is happening should be given.
You have three sets of numbers you need to generate.
The first number has the most requirements. It has to be greater than 100 but not contain an 8 or 9.
You ensure it will always be greater than 100 by using:
(rand.nextInt(7)+1) * 100.
This says, generate a random number between 0 and 6. Add 1 to that number to ensure that it can never be 0. So if it picks 0, +1 is added making it 1. If it picks 6, +1 is added making it 7, etc. This satisfies rule #1 and rule #2.
You ensure the first number never has an 8 or 9.
(rand.nextInt(8) * 10) + rand.nextInt(8)
Genreate a random number from 0-7. The *10 makes sure it will be in the tenth position while the last one places the number in the last position.
Instead of trying to fix the other answer as it also uses DecimalFormat incorrectly.
package stackoverflow_4574713;
import java.text.DecimalFormat;
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random rand = new Random();
int num1 = (rand.nextInt(7) + 1) * 100 + (rand.nextInt(8) * 10) + rand.nextInt(8);
int num2 = rand.nextInt(743);
int num3 = rand.nextInt(10000);
DecimalFormat df3 = new DecimalFormat("000"); // 3 zeros
DecimalFormat df4 = new DecimalFormat("0000"); // 4 zeros
String phoneNumber = df3.format(num1) + "-" + df3.format(num2) + "-" + df4.format(num3);
System.out.println(phoneNumber);
}
}
Output:
662-492-1168
For the first three digits, you need to generate each digit separately. See variables i1, i2, and i3 below.
For the three digits, any number between 0 and 741 should work.
For the final set of four digits, any number between 0 and 9999 should work.
The trick here is how you format the output. You could do it with a NumberFormat
object, but I chose to do it with the String.format()
method. In it, you specify how you want each number to be formatted. So, I used the format string "%d%d%d-%03d-%04d"
. The %d
inserts a base-10 formatted integer into the string. The %03d
makes sure that it is three characters wide and that any additional space is left-padded with a 0
. In other words, 4
is formatted as "004"
and 27
is formatted as "027"
. The %04d
works similarly, except it is four characters wide.
Here's how you put it all together.
Random r = new Random(); int i1 = r.nextInt(8); // returns random number between 0 and 7 int i2 = r.nextInt(8); int i3 = r.nextInt(8); int i4 = r.nextInt(742); // returns random number between 0 and 741 int i5 = r.nextInt(10000); // returns random number between 0 and 9999 String phoneNumber = String.format("%d%d%d-%03d-%04d", i1, i2, i3, i4, i5); System.out.println(phoneNumber);`
Hmm, just a really simple idea to change this to
num1 = rand.nextInt(8)*100 + rand.nextInt(8)*10 + rand.nextInt(8);
num2 = rand.nextInt(743);
For easier output you may use DecimalFormat
DecimalFormat df3 = new DecimalFormat ( "000" ); // 3 zeros
DecimalFormat df4 = new DecimalFormat ( "0000" ); // 4 zeros
System.out.println(df3.format(num1)+"-"+df3.format(num2)+"-"+df4.format(num3));
I had this same question as the first assignment for my Java class, with the caveat that we could only use the methods that we have learned in class up to that point. Therefore, we could not use the .format method. Here is what I came up with:
import java.util.Random;
/**
*
* @author
*/
public class Randnum {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Random num = new Random();
int num0, num1, num2, num3, num4, num5, num6, num7;
num0 = num.nextInt(7) + 1;
num1 = num.nextInt(8);
num2 = num.nextInt(8);
num3 = num.nextInt(643) + 101;
num4 = num.nextInt(10);
num5 = num.nextInt(10);
num6 = num.nextInt(10);
num7 = num.nextInt(10);
String randnum= "A random phone number: ";
System.out.print (randnum);
System.out.print (num0);
System.out.print (num1);
System.out.print (num2);
System.out.print ("-" + num3 + "-");
System.out.print (num4);
System.out.print (num5);
System.out.print (num6);
System.out.println (num7);
}
}
You might also want to check that the prefix is not 555
var num1 = Math.floor(Math.random()*7) + 1;
var num2 = Math.floor(Math.random()*8);
var num3 = Math.floor(Math.random()*8);
var part2 = Math.floor(Math.random()*742);
if(part2 < 100){
part2 = "0" + part2;
}else if (part2 < 10) {
part2= "00" + part2;
}else{
part2 = part2;
}
var part3 = Math.floor(Math.random()*10000);
if(part3 < 1000){
part3 = "0" + part3;
}else if (part3 < 100) {
part3 = "00"+ part3;
}else if(part3 < 10){
part3 = "000" + part3;
}else{
part3 = part3;
}
document.getElementById("demo").innerHTML= " " + num1 + num2 + num3 + "-" + part2 + "-" + part3;
public String GetRandomPhone(){
return String.format("(%03d) %03d-%04d",
(int) Math.floor(999*Math.random()),
(int) Math.floor(999*Math.random()),
(int) Math.floor(9999*Math.random()));
}
精彩评论