我试图检查用户输入是否等于我的数组中的数据, 然后打印与字符串数组对应的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]);
}
}
答案 0 :(得分:1)
当我自己运行这段代码时,所有人似乎都在工作。我认为你的逻辑没有错。你必须在那里有一个拼写错误。
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]);
}
}
}
答案 1 :(得分:0)
您可能想要尝试equalsIgnoreCase()
而不是equals()
,并且可能还想在for循环中添加break
语句,以便在字符串匹配时停止迭代。
答案 2 :(得分:0)
您可以将数组转换为列表并使用该方法。 list.contains() Arrays.asList(firstNameLetter)。载(姓名);
所有给定输入的等等
答案 3 :(得分:0)
作为一项完整性检查,您是否能够成功重新编译?在尝试运行您的解决方案时,我发现Scanner
实例未被声明。添加以下内容:
Scanner myScan = new Scanner(System.in);
允许我使用以下输出运行:
$java ScannerTest
What is your first name?
Henry
What is your middle initial?
A
380
1
答案 4 :(得分:0)
您的程序运行正常(除了您可能已经丢失并且没有重复的Scanner myScan = new Scanner(System.in);
之外)。
你可能会感到困惑的是,你已经将几个整数定义为八进制值。当您开始一个0
值的整数时,它被声明为八进制。
例如,对应于Albert的第二个值是020
。这是16的八进制值。
正如其他人所提到的,您可能希望在字符串上使用equalsIgnoreCase()
并在循环内使用break;
。同样具有两个相等长度的阵列是危险的;更好地将值成对存储在HashMap
或其他结构中。
答案 5 :(得分:0)
if(str.toLowerCase().equals())