gpt4 book ai didi

Java 数组将输入与数组匹配

转载 作者:行者123 更新时间:2023-12-01 23:03:19 26 4
gpt4 key购买 nike

我正在尝试在类中编写一个方法,提示用户输入学生的姓名。然后在现有名称列表中搜索匹配项。但我似乎无法弄清楚如何继续编码以搜索有效的匹配项。

 public void modifyExam(String [] names)
{
String name;
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the name of the student whose grade you would like to modify: ");
name = scanner.nextLine();
boolean nameMatch = true;
for (int i=0; i<names.length; i++)
{
// ....
}

最佳答案

您应该使用 .equals() 来比较 Java 中的字符串。示例:

public void modifyExam(String [] names) {
String name;
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the name of the student whose grade you would like to modify: ");
name = scanner.nextLine();
boolean nameMatch = false;
for (int i=0; i<names.length; i++) {
if( names[i].equals(name) ) {
// do your logic here ...
}
}
}

关于Java 数组将输入与数组匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23187254/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com