java - 如何检查数组中是否找到用户输入?

标签 java arrays

我正在尝试检查用户输入是否等于我的数组中的数据, 然后打印与字符串数组对应的int数组, 但当我运行该程序时什么也没有出现。

我做错了什么?

这是我的主要方法中的所有内容:

  String firstname;
  String middleinitial;

  System.out.println("What is your first name?");
  firstname = myScan.nextLine();

  System.out.println("What is your middle initial?");
  middleinitial = myScan.nextLine();

 String[] firstNameLetter = {"A", "Albert", "Alice", "Ann", "Anna", "Annie",        "Arthur", 
            "B", "Bernard", "Bette", "Bettie", "Betty", "C", "Carl",
            "Catherine", "Charles", "Clara", "D", "Donald", "Dorothy, E",
            "Edward", "Elizabeth", "F", "Florence", "Frank", "G", "George",
            "Grace", "H", "Harold", "Harriet", "Harry", "Hazel", "Helen",      "Henry",
            "I", "J", "James", "Jane", "Jayne", "Jean", "John", 
            "Joan", "Joseph", "K", "L", "M", "Margaret", "Martin",
            "Marvin", "Mary", "Melvin", "Mildred", "N", "O", "P",
            "Patricia", "Paul", "Q", "R", "Richard", "Ruby", "Robert",
            "Ruth", "S", "T", "Thelma", "Thomas", "U", "V",
            "W", "Walter", "Wanda", "William", "Wilma", "X", "Y",
            "Z"};

    int[] firstNameNumber     = {000, 020, 020, 040, 040, 040, 040,
             060, 80, 80, 80, 80, 100, 120,
             120, 140, 140, 160, 180, 180, 200,
             220, 220, 240, 260, 260, 280, 300,
             300, 320, 340, 340, 360, 360, 380, 380,
             400, 420, 440, 440, 440, 460, 460,
             480, 480, 500, 520, 540, 560, 560,
             580, 580, 600, 600, 620, 640, 660,
             680, 680, 700, 720, 740, 740, 760,
             760, 780, 800, 820, 820, 840, 860,
             880, 900, 900, 920, 920, 940, 960,
             980};

String[] middleNameLetter = {" ", "A", "B", "C", "D", "E", "F",
             "G", "H", "I", "J", "K", "L", "M",
             "N", "O", "P", "Q", "R", "S", "T",
             "U", "V", "W", "X", "Y", "Z"};

int[] middleNameNumber    = {0, 1, 2, 3, 4, 5, 6,
                             7, 8, 9, 10, 11, 12, 13,
                             14, 14, 15, 15, 16, 17, 18,
                             18, 18, 19, 19, 19, 19};

for (int count = 0; count < firstNameLetter.length; count++)
{
    if (firstname.equals(firstNameLetter[count]))
    {
        System.out.println(firstNameNumber[count]);
    }
}

for (int count = 0; count < middleNameLetter.length; count++)
{
    if (middleinitial.equals(middleNameLetter[count]))
    {
        System.out.println(middleNameNumber[count]);
    }
}

最佳答案

当我自己运行这段代码时,一切似乎都正常。我看你的逻辑没有什么问题。您一定在某个地方有错别字。

public static void main(String[] args) {

    Scanner myScan = new Scanner(System.in);
    String firstname;
    String middleinitial;

    System.out.println("What is your first name?");
    firstname = myScan.nextLine();

    System.out.println("What is your middle initial?");
    middleinitial = myScan.nextLine();

    String[] firstNameLetter = {"A", "Albert", "Alice", "Ann", "Anna", 
        "Annie","Arthur", "B", "Bernard", "Bette", "Bettie", "Betty", 
        "C", "Carl", "Catherine", "Charles", "Clara", "D", "Donald", 
        "Dorothy, E", "Edward", "Elizabeth", "F", "Florence", "Frank", 
        "G", "George", "Grace", "H", "Harold", "Harriet", "Harry", "Hazel",
        "Helen", "Henry", "I", "J", "James", "Jane", "Jayne", "Jean", 
        "John", "Joan", "Joseph", "K", "L", "M", "Margaret", "Martin",
        "Marvin", "Mary", "Melvin", "Mildred", "N", "O", "P",
        "Patricia", "Paul", "Q", "R", "Richard", "Ruby", "Robert",
        "Ruth", "S", "T", "Thelma", "Thomas", "U", "V",
        "W", "Walter", "Wanda", "William", "Wilma", "X", "Y",
        "Z"};

    int[] firstNameNumber = {000, 020, 020, 040, 040, 040, 040,
                             060, 80, 80, 80, 80, 100, 120,120,
                             140, 140, 160, 180, 180, 200, 220, 
                             220, 240, 260, 260, 280, 300, 300,
                             320, 340, 340, 360, 360, 380, 380,
                             400, 420, 440, 440, 440, 460, 460,
                             480, 480, 500, 520, 540, 560, 560,
                             580, 580, 600, 600, 620, 640, 660,
                             680, 680, 700, 720, 740, 740, 760,
                             760, 780, 800, 820, 820, 840, 860,
                             880, 900, 900, 920, 920, 940, 960, 980};  

    String[] middleNameLetter = {" ", "A", "B", "C", "D", "E", "F",
                                 "G", "H", "I", "J", "K", "L", "M",
                                 "N", "O", "P", "Q", "R", "S", "T",
                                 "U", "V", "W", "X", "Y", "Z"};

    int[] middleNameNumber = {0, 1, 2, 3, 4, 5, 6,
                              7, 8, 9, 10, 11, 12, 13,
                              14, 14, 15, 15, 16, 17, 18,
                              18, 18, 19, 19, 19, 19};

    for (int count = 0; count < firstNameLetter.length; count++) {
        if (firstname.equals(firstNameLetter[count])) {
            System.out.println(firstNameNumber[count]);
        }
    }

    for (int count = 0; count < middleNameLetter.length; count++) {
        if (middleinitial.equals(middleNameLetter[count])) {
            System.out.println(middleNameNumber[count]);
        } 
    }
}

关于java - 如何检查数组中是否找到用户输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37337368/

相关文章:

java - (Gridbag 布局)图形组件被剪切到另一个网格中,不知道为什么

java - 如何在自定义对象类中设置 @ServerTimestamp 注释 - Firestore

java - Spring MVC 3 模糊映射

arrays - Julia:如何在多维数组中逐行迭代

iphone - 数组中缺少 UIView

java - 使用 RSAL Toolkit 的 SSL 未被 jar 错误封装

java - 大写问题,使用 IndexOf

javascript - 成为真正数组的条件是什么

python - 将 JSON 文档展平为单行

perl - 如何在处理完 Perl 数组中的元素后删除它?