我正在开发一个简单的程序来确定用户的输入是否是一个8字符的预定义数组。如果是,则应打印出字母及其位置。如果没有,它应该说它不存在。
问题在于它通过数组查找并找到该字符,然后转到其他地方并打印出它并不知道"由于for循环,它在7个剩余位置的位置。我在纸上追踪变量和条件,虽然我有点理解为什么会出现问题,但我真的不知道如何解决它。
到目前为止,我已经尝试打破循环,创建一个布尔值,如果找到该角色就会改变并打印出来的信息(仍然是它的问题转移到其他地方并打印7次,它可以&#39 ;找到它。)
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 :(得分: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");
}
}
答案 1 :(得分:0)
你需要将else语句放在for循环之外,因为每次它通过for语句中的“i”查找并且找不到该字符时它将打印“else”,所以“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");
答案 2 :(得分:0)
1)你的“其他”是在错误的地方,
2)你可以使用“for()”循环中的“break”:
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");
}
答案 3 :(得分: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。
答案 4 :(得分:0)
你的代码有很多错误,至少在我的电脑上是这样。你的&#34;其他&#34;声明是好的,但实施是错误的,这给了&#34;找不到&#34;在&#34; FOUND&#34;之后信,这是工作代码:
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;
如果for循环找到了char,并且在它循环通过for循环之前,甚至在找到与其他char不匹配的char并转到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");
}
}