2014-11-23 33 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"); 
    } 
}