사용자 입력이 미리 정의 된 8 자의 배열에 있는지를 결정하는 간단한 프로그램을 만들고 있습니다. 그럴 경우 문자와 위치를 인쇄해야합니다. 그렇지 않다면 그것이 존재하지 않는다고 말해야합니다.for 루프를 어떻게 제어 할 수 있습니까?
문제는 배열을 살펴보고 문자를 찾은 다음 else로 이동하여 for 루프로 인해 나머지 위치 7 개가 어디에 있는지 "알 수 없다"는 것을 인쇄합니다. 종이의 변수와 조건을 추적 해 보았습니다. 문제가 발생하는 이유를 다소 이해하지만 실제로 해결 방법을 모르겠습니다.
지금까지 루프를 깨고 캐릭터가 발견되어 정보를 인쇄하면 변경 될 수있는 부울을 만들려고했습니다. (여전히 문제는 다른 사람에게 가서 7 번 인쇄 할 수 없습니다. 그것).
char search;
char tryAgain='Y';
char arrayChar[]= {'a', 'd', 'g', 'h', 'r', 't', 'u', 'y'};
while(tryAgain == 'Y')//while user enters Y
{
System.out.println("\nEnter a character to check if it is present in the array");
search= input.nextLine().charAt(0);//extract first letter
for(int i = 0; i < arrayChar.length; i++)//looks through
{
if(search == arrayChar[i])//if it is found
System.out.println(search + " was found in the " + i +" position\n");//print that it is
else
System.out.println("Cannot find the character");
}
System.out.println("Would you like to try again? Yes/No?");
tryAgain= input.nextLine().charAt(0);
}//while try again
System.out.println("Thank you come again");