我正在尝试在Java中实现一种简单的挂起情况,其中初始化一个字符数组,接受用户输入,并返回字符串输入中字母出现的所有索引。到目前为止,我有一个非常奇怪的输出,我认为这是由于我的初学者在我的循环和某些位置方面的失礼。如果您对这个问题有洞察力,请告诉我:
public class ArrayRandomString {
public static void main(String[] args) {
// Create an array of characters:
Character[] anArray = { 'P', 'A', 'P', 'A', 'B', 'E', 'A', 'R' };
for (char ch : anArray)
System.out.print(ch + " ");
System.out.println();
System.out.println("This program initializes a secret word or phrase to be guessed by you!");
System.out.println("Enter a string of less than 5 characters to return all indexes where it occurs in scrambled string: ");
Scanner input = new Scanner(System.in);
String string = input.next();
System.out.println(string);
System.out.println(Character.toUpperCase(string.charAt(0)));
List<Integer> myList = new ArrayList<Integer>();
List<Integer> noList = new ArrayList<Integer>();
int i;
int j;
// Loop to find index of inputted character
for (i = 0; i < anArray.length; i++) {
Character ch = new Character(anArray[i]);
for (j = 0; j < string.length(); j++) {
List<Integer> letterList = new ArrayList<Integer>();
if (Character.toUpperCase(string.charAt(j)) == ch) {
letterList.add(j);
}
System.out.println(Character.toUpperCase(string.charAt(j)) + " occurs at the following index " + letterList);
}
}
// System.out.println("Your letter occurs at the following index in the scrambled array. No occurence, if empty: " + myList);
}
}
如果用户输入为'PEAR', 理想输出:
P occurs at [0, 2]
E occurs at [5]
A occurs at [1, 3, 6]
R occurs at [7]
如果用户输入为'TEA', 理想输出:
T does not occur in string
E occurs at [5]
A occurs at [1, 3, 6]
到目前为止,我没有编写“不会发生”,因为“确实发生”已经很奇怪了。提前致谢
答案 0 :(得分:1)
你的内部外部循环混淆了,你有时使用错误的数组/字符串使用错误的索引(i或j)。它有助于为i
和j
提供更具描述性的名称,以便您知道何时使用哪个。
最后,你只能打印&#34;发生&#34;在您尝试查找所有匹配项之后的字符串,否则您将为每次出现打印整行,而不是为所有出现的字符打印一行。
这会修复你的循环:
for (int stringIndex = 0; stringIndex < string.length(); stringIndex++) {
char ch = Character.toUpperCase(string.charAt(stringIndex));
List<Integer> letterList = new ArrayList<Integer>();
for (int anArrayIndex = 0; anArrayIndex < anArray.length; anArrayIndex++) {
if (anArray[anArrayIndex] == ch) {
letterList.add(anArrayIndex);
}
}
System.out.println(ch + " occurs at the following index " + letterList);
}
我将此作为练习让自己测试letterList
是否为空(提示:查看方法isEmpty()
的Javadoc)并打印另一条消息以表明您没有&# 39;找不到任何事件。
答案 1 :(得分:0)
int i;
// Loop to find index of inputted character
// List to store the output
List<String> outList = new ArrayList<String> ();
char[] stringArray = string.toCharArray();
for (char stringArrayChar: stringArray) {
// StringBuilder to store output for each iteration
StringBuilder sb = new StringBuilder();
sb.append(Character.toUpperCase(stringArrayChar) + " occurs at ");
// positions List
List<Integer> posList = new ArrayList<Integer>();
i = 0;
for (char anArrayChar: anArray) {
if (anArrayChar == Character.toUpperCase(stringArrayChar)) {
posList.add(i);
}
i++;
}
// Check if the char is present in the specified array
if (posList.size() > 0) {
sb.append(posList.toString());
} else {
sb.append(" No Place in Array");
}
outList.add(sb.toString());
}
// Print the final result
for(String outString: outList ) {
System.out.println(outString);
}