Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:组合排序函数计算错误的结果_Java_Numbers_Repeat - Fatal编程技术网

Java:组合排序函数计算错误的结果

Java:组合排序函数计算错误的结果,java,numbers,repeat,Java,Numbers,Repeat,我有一个程序,可以生成6行数字(整数数组)。 我将输出传递给另一个程序,该程序使用BubbleSort算法对其进行排序,并将其写入文本文件。 如果使用第一个程序时没有通过,那么它工作正常,没有重复的数字,也没有零。但是当排序时,有重复的数字,甚至我也看到了零,零的情况我不能重现,而是双发生的数字。它可能与多线程/并行处理或执行它的环境有关,这些环境由amd多核win 10主机和deb jessie guest组成 java-LottoArray | java-BubbleSort>test2.t

我有一个程序,可以生成6行数字(整数数组)。 我将输出传递给另一个程序,该程序使用BubbleSort算法对其进行排序,并将其写入文本文件。 如果使用第一个程序时没有通过,那么它工作正常,没有重复的数字,也没有零。但是当排序时,有重复的数字,甚至我也看到了零,零的情况我不能重现,而是双发生的数字。它可能与多线程/并行处理或执行它的环境有关,这些环境由amd多核win 10主机和deb jessie guest组成

java-LottoArray | java-BubbleSort>test2.txt
//终端

test2.txt

2 13 16 20 27 40 
9 14 17 21 25 41 
6 11 11 19 27 44 
4 10 25 34 39 47 
11 12 17 36 44 48 
1 15 23 31 39 40 
3 22 22 23 33 45 
1 25 26 26 35 49 
11 14 24 25 41 49 
6 6 14 17 38 46 
4 19 19 28 35 39
正如你所看到的,在最后一行之前的那一行中的6是双精度的,22和11是双精度的

public class LottoArray{

    public static void main (String [] args){

    for(int o=0;o<=10;o++){
        int Reihe [] = new int [6];
        int zahl;

        int j=0;
        int i= 0;

        while(j<Reihe.length){
            zahl = (int) (Math.random()*50);
             boolean schonda = false;
            while ( i<j){
                if(Reihe[i]== zahl) 
                    schonda=true;
                i++;
            }

            if(schonda==false && zahl !=0){
                Reihe[j]=zahl;
                j++;}
        }

       for(int z=0;z<6;z++){
            System.out.print(Reihe[z]+" ");
    }   
    System.out.println();

    }  

   }
}

有人遇到过这种不应该出现的数字吗?

你的问题就在这一块彩票中:

int j=0;
int i= 0;

while(j<Reihe.length){
    zahl = (int) (Math.random()*50);
    boolean schonda = false;
    while ( i<j){
        if(Reihe[i]== zahl) 
            schonda=true;
        i++;
    }

    if(schonda==false && zahl !=0){
        Reihe[j]=zahl;
        j++;
    }
}
intj=0;
int i=0;

虽然(JHI,即使你似乎知道用<代码>标记> <代码>的样式文本。你的问题有点混乱,我更正,认为它更容易阅读。对于将来的问题,你可以在这里查找如何用<代码>标记> <代码>:非常感谢你的更正,看起来比我做的要多。我会试着考虑下一个TI。mes。即使现在读起来更容易,但似乎没有人知道出了什么问题。不客气,也许你现在更幸运了,我还是改变了标题;-)谢谢你的回答,我觉得这很合理。我应该亲自去看看,因为这种错误很常见。但是在最后一部分,你假设我初始化
i
,它是整数类型,使用
schonda
这是布尔值。我会在(我的意思是你应该把
I
的初始化放在
schonda
的同一个地方(上面或下面一行),不是说你应该使用这个值来做这件事。
int i=0
很好。首先谢谢。现在在第一个while循环中初始化“i”,它就工作了。但是我不明白为什么。即使“i”现在在第一个while循环中初始化,它仍然在第一次迭代“j”之上,因此第一次进入循环时th'i'和'j'应该仍然是零。
15 2 20 5 26 34 
13 6 15 33 12 37 
44 17 16 23 40 25 
25 47 10 43 40 44 
25 29 3 30 10 41 
32 1 23 35 43 28 
9 34 28 32 33 25 
5 46 31 16 25 9 
9 13 16 18 40 5 
29 15 16 2 16 15 
34 33 44 13 43 48
int j=0;
int i= 0;

while(j<Reihe.length){
    zahl = (int) (Math.random()*50);
    boolean schonda = false;
    while ( i<j){
        if(Reihe[i]== zahl) 
            schonda=true;
        i++;
    }

    if(schonda==false && zahl !=0){
        Reihe[j]=zahl;
        j++;
    }
}