无限循环问题,在java中比较数组

发布于 2024-12-24 17:06:34 字数 792 浏览 1 评论 0原文

我正在尝试用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

寂寞花火° 2024-12-31 17:06:34

您在 while 语句周围缺少 {},但这并不是您遇到的唯一问题。

我假设 result 也是一个 char 数组,但我看不到您在代码中声明它的位置。如果它是一个 char 数组,则无法使用 stringarray != result 来比较它,因为它始终为 false,从而导致另一个无限循环。

您需要使用 Arrays.equals(array1, array2); 以便在数组之间进行比较。

将 while 语句更改为:

while(!Arrays.equals(stringarray, result)) {
  //code here
}

您将避免另一个无限循环。

You are missing the {} around your while 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 using stringarray != 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:

while(!Arrays.equals(stringarray, result)) {
  //code here
}

And you will avoid another infinite loop.

秋心╮凉 2024-12-31 17:06:34

就是这个家伙!

while(stringarray != result)

        count++;   

我猜你错过了 {........}while()

This is the guy !

while(stringarray != result)

        count++;   

I guess you missed {........} for your while()

小ぇ时光︴ 2024-12-31 17:06:34

这里是你的无限循环:

while(stringarray != result)

    count++;

因为没有大括号,只有下一行被认为是循环的一部分,所以在这个 while 循环中,除了增加计数值之外,什么也没有发生。

做必要的意愿吧。就这样

问候

Here lies your infinite loop :

while(stringarray != result)

    count++;

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

高冷爸爸 2024-12-31 17:06:34

因为在 while 语句后面缺少花括号来封闭 for 循环。

Because you are missing curly braces after the while statement to enclose the for loop.

最舍不得你 2024-12-31 17:06:34

使用带有花括号的 while 循环并使用 equals 方法,如下所示。

while(!Arrays.equals(stringarray, result))
{
   //...
}

Arrays.equals() 方法定义如下。

public static boolean equals(Object[] a, Object[] a2)

如果两个指定的对象数组彼此相等,则返回true。如果两个数组包含相同数量的元素,并且两个数组中所有对应的元素对都相等,则认为这两个数组相等。两个对象 e1e2 被视为相等 if (e1==null ? e2==null : e1.equals(e2))。换句话说,如果两个数组包含相同顺序的相同元素,则它们相等。此外,如果两个数组引用都为 null,则认为两个数组引用相等。

Use the while loop with curly braces and and use the equals method as follows.

while(!Arrays.equals(stringarray, result))
{
   //...
}

The Arrays.equals() method defines as follows.

public static boolean equals(Object[] a, Object[] a2)

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 objects e1 and e2 are considered equal if (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 are null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文