我有一个由名字组成的数组。我有一个搜索功能,搜索数组的元素,它工作正常。但是,对于数组的多个元素,我无法找到如何打印返回的结果数。例如,如果找到我的示例中的“John”,它就是,我不知道如何显示找到了多个结果。有人请帮帮我。我需要为每个找到的结果“计数”增加1次。这是我的代码: `
import java.util.*;
class Search {
public static void main(String[] args) {
Scanner search = new Scanner(System.in);
String[] firstName = new String[]{"John", "Thomas", "Samuel", "Chris", "Daniel", "Joey", "David", "Joshua", "Michael", "John"};
Arrays.sort(firstName);
System.out.print("Enter a search term: ");
String name = search.next();
int i;
boolean foundIt = false;
search:
for (i = 0; i < firstName.length; i++) {
if (firstName[i] == name) {
foundIt = true;
}
}
if (foundIt = true){
System.out.println("Your search term of " + name + " produced " + count + " search results");
}
else {
System.out.println("Your search term of " + name + " did not return any results");
}
}
}
答案 0 :(得分:1)
您可以将boolean foundIt更改为int count,并在将foundIt设置为true的位置增加。
类似于:
int count = 0;
search:
for (i = 0; i < firstName.length; i++) {
if (firstName[i] == name) {
count++;
}
}
if (count > 0){
System.out.println("Your search term of " + name + " produced " + count + " search results");
}
else {
System.out.println("Your search term of " + name + " did not return any results");
}
答案 1 :(得分:0)
试试这段代码:
Scanner search = new Scanner(System.in);
String[] firstName = {"John", "Thomas", "Samuel", "Chris", "Daniel", "Joey", "David", "Joshua", "Michael", "John"};
Arrays.sort(firstName);
System.out.print("Enter a search term: ");
String name = search.next();
int count = 0;
for (String aFirstName : firstName) {
if (aFirstName.equals(name)) {
count++;
}else if(count > 0){
break;
}
}
if(count > 0){
System.out.println("Your search term of " + name + " produced " + count + " search results");
}else{
System.out.println("Your search term of " + name + " did not return any results");
}
答案 2 :(得分:0)
您可以将结果添加到ArrayList,然后更容易理解。此外,您在if语句中分配变量,并在字符串上使用==
运算符,您应该使用.equals()
。
另一个突出的问题是代码中的标签,你应该远离它们,因为它们需要使用臭名昭着的break
语句。见Dijkstra的considered harmful.
这样你也可以在之后迭代找到的名字。
参见示例:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
class Search {
public static void main(String[] args) {
Scanner search = new Scanner(System.in);
String[] firstNames = new String[]{
"John",
"Thomas",
"Samuel",
"Chris",
"Daniel",
"Joey",
"David",
"Joshua",
"Michael",
"John"};
Arrays.sort(firstNames);
System.out.print("Enter a search term: ");
String name = search.next();
List<String> results = new ArrayList<String>();
for (String s : firstNames) {
if (s.equals(name)) {
results.add(s);
}
}
if (results.size() > 0) {
System.out.println("Your search term of " + name + " produced " + results.size() + " search results");
} else {
System.out.println("Your search term of " + name + " did not return any results");
}
}
}
答案 3 :(得分:0)
private void searchName(){
Employee[]e=toEmployee();
for(int i=0;i<em.size();i++){
if(e[i].getName().equals(tfs.getText())){
tfi.setText(e[i].getId());
tfn.setText(e[i].getName());
tfh.setText(e[i].getHour()+"");
tfr.setText(e[i].getRate()+"");
}
}
}