我有以下程序,其中用户输入他的社会安全号码并被分配一个与之对应的位置。
我没有得到的一件事是我如何添加一个循环来搜索我的数组,直到它找到正确的区号并且它对应的区域例如3对应于新汉普郡。我在程序中尝试了一个循环,但我不确定如何使它工作。
import jpb.*;
public class SSNInfo
{
public static void main(String[] args)
{
SimpleIO.prompt("Enter a Social Security number: ");
String ssn = SimpleIO.readLine().trim();
while (true)
{
if (ssn.length() != 11)
{
System.out.println("Error: Number must have 11 " +
"characters");
}
else if (ssn.charAt(3) != '-' ||
ssn.charAt(6) != '-')
{
System.out.println(
"Error: Number must have the form ddd-dd-dddd");
}
else
break;
SimpleIO.prompt("\nPlease re-enter number: ");
ssn = SimpleIO.readLine().trim();
}
// Get the area number (the first 3 digits of the SSN)
int area = Integer.parseInt(ssn.substring(0, 3));
int[] areaNumbers =
{ 3, 7, 9, 34, 39, 49, 134, 158, 211, 220,
222, 231, 236, 246, 251, 260, 267, 302, 317, 361,
386, 399, 407, 415, 424, 428, 432, 439, 448, 467,
477, 485, 500, 502, 504, 508, 515, 517, 519, 520,
524, 525, 527, 529, 530, 539, 544, 573, 574, 576,
579, 580, 584, 585, 586, 588, 595, 599, 601, 626,
645, 647, 649, 653, 658, 665, 675, 679, 680};
String[] locations =
{"New Hampshire", "Maine", "Vermont",
"Massachusetts", "Rhode Island", "Connecticut",
"New York", "New Jersey", "Pennsylvania",
"Maryland", "Delaware", "Virginia",
"West Virginia", "North Carolina", "South Carolina",
"Georgia", "Florida", "Ohio",
"Indiana", "Illinois", "Michigan",
"Wisconsin", "Kentucky", "Tennessee",
"Alabama", "Mississippi", "Arkansas",
"Louisiana", "Oklahoma", "Texas",
"Minnesota", "Iowa", "Missouri",
"North Dakota", "South Dakota", "Nebraska",
"Kansas", "Montana", "Idaho",
"Wyoming", "Colorado", "New Mexico",
"Arizona", "Utah", "Nevada",
"Washington", "Oregon", "California",
"Alaska", "Hawaii", "District of Columbia",
"Virgin Islands", "Puerto Rico", "New Mexico",
"Pacific Islands", "Mississippi", "Florida",
"Puerto Rico", "Arizona", "California",
"Texas", "Utah", "New Mexico",
"Colorado", "South Carolina", "Louisiana",
"Georgia", "Arkansas", "Nevada"};
}
int i;
for (i = 0; i < areaNumbers.length; i++)
if areanumbers[i] == int area;
break;
}
答案 0 :(得分:2)
为什么不尝试HashMap<Integer, String>
?使用areaNumber作为键,将location作为值。您不需要遍历areaNumbers,只需使用map.get() - 方法。
修改强> 如果您想使用循环,请使用该循环:
for(int i = 0;i<areaNumbers.length;i++){
if(areaNumbers[i] == area){
String location = locations[i];
// you found the right location, do what you want with it ;-)
break;
}
}
答案 1 :(得分:2)
我建议您创建一个区域信息的HashMap。 Map对象允许您将美国州或地区映射到与您解析的数字相对应的数字。
Map<Integer, String> areaMap = new HashMap<Integer, String>();
areaMap.put(new Integer(3), "New Hampshire");
areaMap.put(new Integer(7), "Maine");
areaMap.put(new Integer(9), "Vermont");
// and so on...
然后使用原始代码......
// Get the area number (the first 3 digits of the SSN)
int area = Integer.parseInt(ssn.substring(0, 3));
String associatedArea = areaMap.get(new Integer(area));
System.out.println("You are from: " + associatedArea);
如果必须使用循环,请尝试以下操作:
String originArea = "";
for(int i=0; i<areaNumbers.length; i++) {
int compareMe = areaNumbers[i];
if( compareMe == area) {
originArea = locations[i];
break;
}
}
System.out.println("You are from: " + originArea);
答案 2 :(得分:1)
Map
使用HashMap
,例如ImmutableMap
甚至Guava
}}。containsKey(Object o)
方法检查它是否在那里,或只是get()
它并检查它是否不是null
。答案 3 :(得分:1)
听起来你在寻找类似的东西:
String location = null;
for (i=0; i < areaNumbers.length; i++)
if areanumbers[i] == area;
location = locations[i];
此位置的结尾将是与区号相关联的位置,如果在areaNumbers
数组中找不到,则为null。
答案 4 :(得分:1)
我能看到的一些事情:
首先,areanumbers[]
与areaNumbers[]
不同,因此您在底部的if
语句中所拥有的内容并未引用您在上面构造的整数数组。
其次,底部的检查中缺少一些代码。你可能想要这样的东西:
int i;
for (i = 0; i < areaNumbers.length; i++)
{
if (areaNumbers[i] == area)
{
// do something with location[i]
}
}
第三,我会看看L Butz提到的其他结构(如果你没有 使用循环,当然!)可能更容易四处。