0

我有以下程序,用户在其中输入他的社会安全号码并分配一个与之对应的位置。

我没有得到的一件事是如何添加一个循环来搜索我的数组,直到找到正确的区域编号并且它对应的区域例如 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;

}
4

5 回答 5

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);
于 2013-10-16T20:46:01.187 回答
2

你为什么不试试HashMap<Integer, String>?使用 areaNumber 作为键,使用位置作为值。您不需要遍历 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;
  }  
}
于 2013-10-16T20:44:05.973 回答
1
  1. 不要将数据存储在数组中,您肯定在使用键值对,因此请使用 a Map,例如 aHashMap甚至ImmutableMap来自Guava.
  2. 然后,让地图使用一种containsKey(Object o)方法来检查它是否存在,或者简单地get()检查它是否不是null.
  3. 您可以格式化您的字符串,无需自己检查。
于 2013-10-16T20:46:46.973 回答
1

在我看来,您正在寻找类似的东西:

String location = null;

for (i=0; i < areaNumbers.length; i++)
    if areanumbers[i] == area;
        location = locations[i];

此位置的末尾将是与区号关联的位置,如果在areaNumbers数组中未找到,则为 null。

于 2013-10-16T20:50:57.257 回答
1

我可以看到一些事情:

首先,areanumbers[]与 不同areaNumbers[],因此您if在底部的语句中所拥有的并不是您在上面构造的整数数组。其次,底部的检查中缺少一些代码。你可能想要这样的东西:

int i;

for (i = 0; i < areaNumbers.length; i++)
{    
    if (areaNumbers[i] == area)
    {
        // do something with location[i]
    }
}

第三,我会研究 L Butz 提到的其他结构(当然,如果您不必使用循环!)可能更容易。

于 2013-10-16T20:59:21.310 回答