2014-11-23 9 views
0

사용자 입력이 미리 정의 된 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"); 

답변

0

루프가 완료되면 Cannot find을 인쇄하십시오. 이처럼 :

import java.io.*; 
import java.util.*; 
import java.lang.*; 

class Example 
{ 
    public static void main(String args []){ 
     char search; 
     Scanner input = new Scanner(System.in); 
     System.out.println("Enter the first number"); 
     //get user input for a 
     char tryAgain='Y'; 

     char arrayChar[]= {'a', 'd', 'g', 'h', 'r', 't', 'u', 'y'}; 


     while(tryAgain == 'Y')//while user enters Y 
     { 
      boolean verify = false; 
      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  
       { 
        verify = true; 
        System.out.println(search + " was found in the " + i +" position\n");//print that it is 
       } 


      } 
      if(verify == false){ 
       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"); 
    } 
} 
0

마다는 "I"는 "다른"인쇄 할 문자를 찾으 문이 아닌의를 통해 보이기 때문에 당신은, for 루프에서 else 문을 배치해야합니다, 그래서 "다른"는 것과 같아야합니다 :

for(int i = 0; i < arrayChar.length; i++) { 

     if(search == arrayChar[i])  

      System.out.println(search + " was found in the " + i +" position\n"); 
    }  
     else 
       System.out.println("Cannot find the character"); 
0

1) 잘못된 장소에 "다른 사람", 그리고

2

) 당신이에서 "휴식"을 사용할 수있다 "(용)" 루프 :

char search; 
    char tryAgain='Y'; 
    char arrayChar[]= { 
     'a', 'd', 'g', 'h', 'r', 't', 'u', 'y' 
    }; 

    //while user enters Y 
    do { 
     System.out.println("\nEnter a character to check if it is present in the array"); 
     search= input.nextLine().charAt(0);//extract first letter 

     //looks through 
     boolean found = false; 
     for(int i = 0; i < arrayChar.length; i++)h { 
     if(search == arrayChar[i]) { 
      System.out.println(search + " was found in the " + i +" position\n"); 
      found = true; 
      break; 
     } 
     } 

     if (!found) { 
     System.out.println("Cannot find the character"); 
     } 

     System.out.println("Would you like to try again? Y/N?"); 
     tryAgain= input.nextLine().charAt(0); 

    } while(java.lang.Character.toUpperCase(tryAgain) == 'Y'); 

    System.out.println("Thank you come again"); 
    } 
0

내가 이해한다면, 발견되면 내부 루프를 중단해야합니까, 그렇지 않으면 계속해야합니까?.

char search; 
    char tryAgain = 'Y'; 
    char arrayChar[] = { 'a', 'd', 'g', 'h', 'r', 't', 'u', 'y' }; 
    boolean found = false; 
    int position = 0; 

    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 

     found = false; 
     position = 0; 

     for (int i = 0; i < arrayChar.length; i++)// looks through 
     { 
      if (search == arrayChar[i]) { // if it is found 
       found = true; 
       position = i; 
       break; 
      } 
     } 

     if (found) { 
      System.out.println(search + " was found in the " + position 
        + " position\n");// print that it is 
      System.out.println("Would you like to play again? Yes/No?"); 
      tryAgain = input.nextLine().charAt(0); 
     } 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"); 

.Ryan :

이보십시오.

0

적어도 내 PC에는 코드에 오류가 많이 있습니다. 귀하의 "다른"문은 좋지만 구현은이 "FOUND"편지 후 "찾을 수 없습니다"주는 잘못이며, 여기에 작업 코드입니다 : for 루프가 발견되면

import java.util.*; 
    import java.io.*; 
    import java.lang.*;; 

    public class Test { 
public static void main(String[] args) { 

    char search; 
    Scanner input = new Scanner(System.in); 
    char tryAgain='Y'; 
    char[] arrayChar = new char[] { 'a', 'd', 'g', 'h', 'r', 't', 'u', 'y' }; 
    boolean flag = false; 

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); 

    for(int i = 0; i < arrayChar.length; i++) { 
     if(search == arrayChar[i]) { 
      flag = true; 
      System.out.println(search + " was found in the " + i +" position\n"); 
      break; 

현재 휴식을 필요로 char, 그리고 다른 것들과 일치하지 않을 char를 찾은 후에 for 루프를 반복하기 전에 else 문으로 가서 else 문을 출력했다.

 } 
     else { flag = false; } 
    } 
    if(flag == false){ 
      System.out.println("Cannot find the character"); 
      //flag = true; 
    } 

    System.out.println("Would you like to try again? Y/N?"); 
     tryAgain = Character.toUpperCase(input.nextLine().charAt(0)); 
     //while user enters Y or y 

}//while try again 

System.out.println("Thank you come again"); 
    } 
} 
관련 문제