我想創建一個範圍內的隨機電話號碼。格式爲(xxx)-xxx-xxx,區號不以0,8或9開頭,下一個三位的範圍是100-742,然後最後一個4位可以是任何數字。我將如何創建前兩個部分?任何幫助,將不勝感激。謝謝!如何用範圍創建一個隨機數?
import java.util.*;
import java.text.*;
public class PhoneNumber{
public static void main(String[] arg){
Random ranNum = new Random();
//int areaCode = 0;
//int secSet = 0;
//int lastSet = 0;
DecimalFormat areaCode = new DecimalFormat("(000)");
DecimalFormat secSet = new DecimalFormat("-000");
DecimalFormat lastSet = new DecimalFormat("-0000");
//DecimalFormat phoneNumber = new DecimalFormat("(###)-###-####");
int i = 0;
//areaCode = (ranNum.nextInt()); //cant start with 0,8,9
//secSet = (ranNum.nextInt()); // not greater than 742 and less than 100
//lastSet = (ranNum.nextInt(999)) + 1; // can be any digits
i = ranNum.nextInt();
System.out.print(areaCode.format(i));
i = ranNum.nextInt();
System.out.print(secSet.format(i));
i = ranNum.nextInt();
System.out.print(lastSet.format(i));
}
}
nextInt需要一個參數:http://docs.oracle.com/javase/7/docs/api/java/util/Random.html#nextInt(int)'AREACODE = nextInt(700)+ 100 ;'開始你。 – Radiodef