我正在尝试检查用户输入是否等于我的数组中的数据, 然后打印与字符串数组对应的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/