java随机生成地址
时间: 2023-07-27 11:15:27 浏览: 268
随机地址生成器
你可以使用Java的随机数生成器和数组来生成一个随机地址。以下是一个简单的示例代码:
```
import java.util.Random;
public class AddressGenerator {
private static final String[] STREET_NAMES = {"Main St.", "Oak St.", "Pine Ave.", "Maple Rd.", "Elm St."};
private static final String[] CITIES = {"New York", "Los Angeles", "Chicago", "Houston", "Phoenix"};
private static final String[] STATES = {"NY", "CA", "IL", "TX", "AZ"};
public static String generateAddress() {
Random random = new Random();
int streetNumber = random.nextInt(1000) + 1;
String streetName = STREET_NAMES[random.nextInt(STREET_NAMES.length)];
String city = CITIES[random.nextInt(CITIES.length)];
String state = STATES[random.nextInt(STATES.length)];
int zipCode = random.nextInt(90000) + 10000;
return streetNumber + " " + streetName + ", " + city + ", " + state + " " + zipCode;
}
public static void main(String[] args) {
System.out.println(generateAddress());
}
}
```
这个程序定义了一个`STREET_NAMES`数组、一个`CITIES`数组和一个`STATES`数组,用于随机生成街道名称、城市和州。`generateAddress()`方法生成一个随机的街道号码、街道名称、城市、州和邮政编码,并将它们组合成一个字符串形式的地址。在`main()`方法中,我们调用`generateAddress()`方法并打印出结果。
阅读全文