2014-11-23 58 views
0

我正在研究一個簡單的程序,以確定用戶的輸入是否在預定義的8個字符的數組中。如果是,那麼它應該打印出該字母及其位置。如果不是,應該說它不存在。我如何控制我的循環

問題是它通過數組查找並找到字符,然後轉到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

您需要將else語句出來的for循環,因爲每次它看起來通過for語句,而不是發現它會打印出「其他」的字符「I」,因此,「其他人」應該是這樣的:

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

您的代碼有很多錯誤,至少在我的電腦上。您的「其他」語句是好的,但執行是錯誤的,它給出了「無法找到」後,「發現」號,這裏的工作代碼:如果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字符並轉到else語句後,然後打印出您的else語句,它仍然在循環之前通過for循環。

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