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回圈遍歷所有元素并在不等于輸入時列印出“Not here names[i]” Scanner。我該如何解決這個問題?
uj5u.com熱心網友回復:
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;
}
uj5u.com熱心網友回復:
如果您希望它僅在根本沒有找到該元素時才在此處列印,則應將其放在 for 回圈之后。因此,如果您找到名稱說您找到了它,那么您將遍歷整個陣列,然后如果您沒有,您可以在此處不列印,這樣的事情:
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 回圈沒有做任何事情。考慮洗掉它或用它做其他事情,因為當您執行第一個回圈時,您第一次直接中斷,所以就好像根本沒有回圈一樣。
uj5u.com熱心網友回復:
您可以創建一個名為的布林值contactFound,并在找到匹配名稱時將其設定為 true。發生這種情況時,您可能還想跳出回圈。在for回圈之外,列印"Not here"ifcontactFound為false,如下圖。
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");
uj5u.com熱心網友回復:
你的第二個 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編輯:使用動態陣列而不是動態陣列更容易做到這一點。這是一個使用ArrayLists 的版本:
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();
uj5u.com熱心網友回復:
匯入 java.util.Arrays;匯入 java.util.Scanner;
公共類 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();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/469090.html
標籤:爪哇 数组 for循环 java.util.scanner
上一篇:查找數字陣列的匹配Python
