Java 向搜索程序添加循环

Java 向搜索程序添加循环,java,Java,我有一个程序,其中一个用户输入他的社会保险号码,并被分配一个与之对应的位置 有一件事我不知道,我如何添加一个循环来搜索我的数组,直到它找到正确的区域号和它对应的区域,例如3对应于新罕布什尔州。我在程序中尝试了一个循环,但我不确定如何使它工作 import jpb.*; public class SSNInfo { public static void main(String[] args) { SimpleIO.prompt("Enter a Social

我有一个程序,其中一个用户输入他的社会保险号码,并被分配一个与之对应的位置

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

}
你为什么不试试HashMap呢?使用区域号作为键,使用位置作为值。您不需要遍历区域编号,只需使用map.get-method即可

编辑: 如果要使用循环,请使用该循环:

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;
  }  
}

我建议创建一个区域信息的哈希图。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...
如果必须使用循环,请尝试以下操作:

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);
不要将数据存储在数组中,您肯定在使用键值,所以请使用映射,例如HashMap,甚至是来自Guava的ImmutableMap。 然后,让映射使用containsKeyObject o方法检查它是否存在,或者简单地获取它并检查它是否不是null。 您可以格式化字符串,无需自己检查。
在我听来,你在寻找这样的东西:

String location = null;

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

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

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

我可以看到以下几点:

首先,areanumbers[]与areanumbers[]不同,因此底部if语句中的内容并不引用上面构造的整数数组。 第二,您的支票底部缺少一些代码。您可能需要以下内容:

String location = null;

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

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

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

请张贴正确格式的代码。人们来这里帮忙,对他们的眼睛要友善!首先,您不能将1声明为变量;可以吗?@小册子我想要基本上当用户输入社会保险号码时,前3个号码是它的区域,要找到它,数组int[]areanumbers你的代码看起来语法错误。您正在对值和语句进行比较。我认为OP会从学习如何使用循环中受益更多。编辑我的问题以提供您需要的内容。是的,我必须将其设置为循环,我认为我了解如何使面积对应于面积数组,但第二部分使我对如何使面积编号匹配感到困惑要使位置数组中的元素匹配,您必须按顺序列出数组元素,以便它们匹配。如果134号区域确实与新泽西州的位置相对应,那么你的状态很好。然后,区号[7]将对应于位置[7],区号[31]将对应于位置[31],等等。这就是为什么我和其他人说,一旦你发现区号[I]与你想要的匹配,就对位置[I]进行处理。