使用 for 循环查找数组中的元素
Finding elements in array using for loop
import java.util.Arrays;
import java.util.Scanner;
public class Grocer2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String[] names = new String[5];
int count = 0;
while(true){
System.out.println("What is your name: ");
// Store scanner input in name
String name = scan.nextLine();
// Add name into array
names[count] = name;
count++;
if(count == 5){
break;
}
}
System.out.println(Arrays.toString(names));
while(true){
System.out.println("Who are you looking for ? ");
String contact = scan.nextLine();
for(int i = 0; i < names.length; i++){
if(names[i].equals(contact)){
System.out.println("They are in aisle " + i);
}else{
System.out.println("Not here");
}
}
break;
}
scan.close();
}
}
我正在尝试将 Scanner
输入添加到数组中,并且我正在尝试使用 for
循环在数组中搜索元素。 for
循环遍历所有元素并在 names[i]
不等于 Scanner
输入时打印出“Not here”。我该如何解决这个问题?
while(true){
System.out.println("Who are you looking for ? ");
String contact = scan.nextLine();
bool isFound = false;
for(int i = 0; i < names.length; i++){
if(names[i].equals(contact)){
System.out.println("They are in aisle " + i);
isFound = true;
break;
}
}
if(!isFound){
System.out.println("Not here");
}
break;
}
如果你想让它只在你根本没有找到元素时不在这里打印,你应该把它放在 for 循环之后。因此,如果您找到名称说您找到了它,那么您将循环遍历整个数组,然后如果您没有找到,您可以在此处打印 not 类似这样的内容:
while(true){
System.out.println("Who are you looking for ? ");
String contact = scan.nextLine();
boolean isThere=false;
for(int i = 0; i < names.length; i++){
if(names[i].equals(contact)){
System.out.println("They are in aisle " + i);
isThere = true;
break;// break only if you want the first time the String appeared
}
}
if(!isThere){
System.out.println("Not here");
}
break;
}
现在应该可以了,但是这里的 while 循环没有做任何事情。考虑删除它或用它做其他事情,因为当你做第一个循环时,你是第一次直接中断,所以就好像根本没有循环一样。
你的第二个 while 循环永远不会循环。您可以在第一个循环中使用 do-while 循环。这是代码的更紧凑版本:
Scanner scan = new Scanner(System.in);
String[] names = new String[5];
int count = 0;
do {
System.out.println("What is your name: ");
// Add name into array
names[count] = scan.nextLine();
count++;
} while (count != 5);
System.out.println(Arrays.toString(names));
System.out.println("Who are you looking for ? ");
String contact = scan.nextLine();
int aisle = -1;
for(int i = 0; i < names.length; i++){
if(names[i].equals(contact)){aisle = i; break;}
}
if (aisle != -1) System.out.println("They are in aisle " + aisle);
else System.out.println("Not here");
scan.close();
编辑:使用 ArrayList
而不是动态数组,这样做要容易得多。这是一个使用 ArrayList
s 的版本:
Scanner scan = new Scanner(System.in);
ArrayList<String> names = new ArrayList<>(5);
for (int i = 0; i<5; i++){
System.out.println("What is your name: ");
names.add(scan.nextLine());
}
System.out.println(names);
System.out.println("Who are you looking for ? ");
String contact = scan.nextLine();
if (names.contains(contact)) System.out.println("They are in aisle " + names.indexOf(contact));
else System.out.println("Not here");
scan.close();
您可以创建一个名为 contactFound
的布尔值,并在找到匹配名称时将其设置为 true。发生这种情况时,您可能还想跳出循环。在 for 循环之外,如果 contactFound
为假,则打印 "Not here"
,如下所示。
boolean contactFound = false;
for (int i = 0; i < names.length; i++) {
if (names[i].equals(contact)) {
System.out.println("They are in aisle " + i);
contactFound = true;
break; // If you want the loop to stop when a matching name has been found
}
}
if (!contactFound) System.out.println("Not here");
import java.util.Arrays;
import java.util.Scanner;
public class Grocer2 {
public static void main(String[] args) {
int count = 0;
int size = 5;
//you should check here
boolean check = false;
Scanner scan = new Scanner(System.in);
String[] names = new String[size];
for (int i = 0; i<size; i++) {
System.out.println("What is your name: ");
// Store scanner input in name
String name = scan.nextLine();
// Add name into array
names[count] = name;
count++;
if (count == size) {
break;
}
}
System.out.println(Arrays.toString(names));
System.out.println("Who are you looking for ? ");
String contact = scan.nextLine();
for (int i = 0; i<names.length; i++) {
if (names[i].equals(contact)) {
System.out.println("They are in aisle " + i);
check = true;
break;
}
}
if (!check) {
System.out.println("Not here");
}
scan.close();
}
}
import java.util.Arrays;
import java.util.Scanner;
public class Grocer2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String[] names = new String[5];
int count = 0;
while(true){
System.out.println("What is your name: ");
// Store scanner input in name
String name = scan.nextLine();
// Add name into array
names[count] = name;
count++;
if(count == 5){
break;
}
}
System.out.println(Arrays.toString(names));
while(true){
System.out.println("Who are you looking for ? ");
String contact = scan.nextLine();
for(int i = 0; i < names.length; i++){
if(names[i].equals(contact)){
System.out.println("They are in aisle " + i);
}else{
System.out.println("Not here");
}
}
break;
}
scan.close();
}
}
我正在尝试将 Scanner
输入添加到数组中,并且我正在尝试使用 for
循环在数组中搜索元素。 for
循环遍历所有元素并在 names[i]
不等于 Scanner
输入时打印出“Not here”。我该如何解决这个问题?
while(true){
System.out.println("Who are you looking for ? ");
String contact = scan.nextLine();
bool isFound = false;
for(int i = 0; i < names.length; i++){
if(names[i].equals(contact)){
System.out.println("They are in aisle " + i);
isFound = true;
break;
}
}
if(!isFound){
System.out.println("Not here");
}
break;
}
如果你想让它只在你根本没有找到元素时不在这里打印,你应该把它放在 for 循环之后。因此,如果您找到名称说您找到了它,那么您将循环遍历整个数组,然后如果您没有找到,您可以在此处打印 not 类似这样的内容:
while(true){
System.out.println("Who are you looking for ? ");
String contact = scan.nextLine();
boolean isThere=false;
for(int i = 0; i < names.length; i++){
if(names[i].equals(contact)){
System.out.println("They are in aisle " + i);
isThere = true;
break;// break only if you want the first time the String appeared
}
}
if(!isThere){
System.out.println("Not here");
}
break;
}
现在应该可以了,但是这里的 while 循环没有做任何事情。考虑删除它或用它做其他事情,因为当你做第一个循环时,你是第一次直接中断,所以就好像根本没有循环一样。
你的第二个 while 循环永远不会循环。您可以在第一个循环中使用 do-while 循环。这是代码的更紧凑版本:
Scanner scan = new Scanner(System.in);
String[] names = new String[5];
int count = 0;
do {
System.out.println("What is your name: ");
// Add name into array
names[count] = scan.nextLine();
count++;
} while (count != 5);
System.out.println(Arrays.toString(names));
System.out.println("Who are you looking for ? ");
String contact = scan.nextLine();
int aisle = -1;
for(int i = 0; i < names.length; i++){
if(names[i].equals(contact)){aisle = i; break;}
}
if (aisle != -1) System.out.println("They are in aisle " + aisle);
else System.out.println("Not here");
scan.close();
编辑:使用 ArrayList
而不是动态数组,这样做要容易得多。这是一个使用 ArrayList
s 的版本:
Scanner scan = new Scanner(System.in);
ArrayList<String> names = new ArrayList<>(5);
for (int i = 0; i<5; i++){
System.out.println("What is your name: ");
names.add(scan.nextLine());
}
System.out.println(names);
System.out.println("Who are you looking for ? ");
String contact = scan.nextLine();
if (names.contains(contact)) System.out.println("They are in aisle " + names.indexOf(contact));
else System.out.println("Not here");
scan.close();
您可以创建一个名为 contactFound
的布尔值,并在找到匹配名称时将其设置为 true。发生这种情况时,您可能还想跳出循环。在 for 循环之外,如果 contactFound
为假,则打印 "Not here"
,如下所示。
boolean contactFound = false;
for (int i = 0; i < names.length; i++) {
if (names[i].equals(contact)) {
System.out.println("They are in aisle " + i);
contactFound = true;
break; // If you want the loop to stop when a matching name has been found
}
}
if (!contactFound) System.out.println("Not here");
import java.util.Arrays;
import java.util.Scanner;
public class Grocer2 {
public static void main(String[] args) {
int count = 0;
int size = 5;
//you should check here
boolean check = false;
Scanner scan = new Scanner(System.in);
String[] names = new String[size];
for (int i = 0; i<size; i++) {
System.out.println("What is your name: ");
// Store scanner input in name
String name = scan.nextLine();
// Add name into array
names[count] = name;
count++;
if (count == size) {
break;
}
}
System.out.println(Arrays.toString(names));
System.out.println("Who are you looking for ? ");
String contact = scan.nextLine();
for (int i = 0; i<names.length; i++) {
if (names[i].equals(contact)) {
System.out.println("They are in aisle " + i);
check = true;
break;
}
}
if (!check) {
System.out.println("Not here");
}
scan.close();
}
}