无限循环问题,在java中比较数组
我正在尝试用Java编写一个非常小的程序,它接受一个字符串作为输入,并使用一个生成伪随机字符的函数,并将它们与字符串的每个元素进行比较,如果它们相同,则字符被写入结果数组与下面的字符串代码中的索引相同,
public static char randomChar(){
int diceroll = dice.nextInt(25);
return alphabet.charAt(diceroll);
}
public static int itterator(String input){
char inputchar;
//convert input string to array
char[] stringarray = input.toCharArray();
while(stringarray != result)
count++;
//itterate each character
for(int i = 0; i<input.length(); i++)
{
inputchar = input.charAt(i);
if( randomChar() == inputchar )
{
result[i] = inputchar;
}
}
return count;
}
但是程序永远不会终止,这是因为我创建了无限循环还是还有其他原因?任何帮助表示赞赏!
干杯
I'm trying to write a really small program in Java that takes a string as input and uses a function that generates a pseudo random chatacter and compares them to each element of the string, if they're the same the character gets written to a result array at the same index that it was in the string code is below,
public static char randomChar(){
int diceroll = dice.nextInt(25);
return alphabet.charAt(diceroll);
}
public static int itterator(String input){
char inputchar;
//convert input string to array
char[] stringarray = input.toCharArray();
while(stringarray != result)
count++;
//itterate each character
for(int i = 0; i<input.length(); i++)
{
inputchar = input.charAt(i);
if( randomChar() == inputchar )
{
result[i] = inputchar;
}
}
return count;
}
The program never terminates however, is this because I have created an infinite loop or is there another reason? Any help is appreciated!
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您在
while
语句周围缺少{}
,但这并不是您遇到的唯一问题。我假设
result
也是一个 char 数组,但我看不到您在代码中声明它的位置。如果它是一个 char 数组,则无法使用 stringarray != result 来比较它,因为它始终为 false,从而导致另一个无限循环。您需要使用 Arrays.equals(array1, array2); 以便在数组之间进行比较。
将 while 语句更改为:
您将避免另一个无限循环。
You are missing the
{}
around yourwhile
statement, but that's not the only issue you're going to have.I'm assuming that
result
is a char array too, but I can't see where you've declared it in your code. If it is a char array, then you cannot compare it usingstringarray != result
as it will always be false, causing another infinite loop.You need to use
Arrays.equals(array1, array2);
in order to do a comparison between arrays.Change your while statement to:
And you will avoid another infinite loop.
就是这个家伙!
我猜你错过了
{........}
的while()
This is the guy !
I guess you missed
{........}
for yourwhile()
这里是你的无限循环:
因为没有大括号,只有下一行被认为是循环的一部分,所以在这个 while 循环中,除了增加计数值之外,什么也没有发生。
做必要的意愿吧。就这样
问候
Here lies your infinite loop :
Since without braces only the first next line is suppose to be the part of the loop, so in this while loop of your nothing is happening except for incrementing count value.
Do the needful will it. That's it
Regards
因为在 while 语句后面缺少花括号来封闭 for 循环。
Because you are missing curly braces after the while statement to enclose the for loop.
使用带有花括号的
while
循环并使用 equals 方法,如下所示。Arrays.equals()
方法定义如下。如果两个指定的对象数组彼此相等,则返回
true
。如果两个数组包含相同数量的元素,并且两个数组中所有对应的元素对都相等,则认为这两个数组相等。两个对象e1
和e2
被视为相等if (e1==null ? e2==null : e1.equals(e2))
。换句话说,如果两个数组包含相同顺序的相同元素,则它们相等。此外,如果两个数组引用都为null
,则认为两个数组引用相等。Use the
while
loop with curly braces and and use the equals method as follows.The
Arrays.equals()
method defines as follows.Returns
true
if the two specified arrays of Objects are equal to one another. The two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. Two objectse1
ande2
are considered equalif (e1==null ? e2==null : e1.equals(e2))
. In other words, the two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both arenull
.