实际上,整个代码的目的是获取数组中的输入值(0或1),然后检查输入的数组中是否有6个连续的0,然后在每6个连续的0之后插入“ 1”。我发现这个街区

if(j>5){
        shift(i+6);
        bits[i+6] = 1;
        count+=1;
        System.out.println(count);
}


即使输入的数组中没有6个连续的0,也会执行该命令。然后检查问题。我添加了此声明

 System.out.println("ABHINAV " + j );


这是输出:-

Entered Bits are:
 1
 0
 1
 0
 1
 0
 1
 0
 1
 0
ABHINAV 0
ABHINAV 1
ABHINAV 0
ABHINAV 1
ABHINAV 0
ABHINAV 1
ABHINAV 0
ABHINAV 1
ABHINAV 0
ABHINAV 6


我发现了问题-变量'j'递增到6,因此输入了'if'块。我的问题是:

“ j”如何增加到6(如您所见,输出捕捉的最后一行)。

该问题如何解决。我做错了。

这是整个代码

class Stuff{
    public static final int LENGTH=6;
    public int count=0;
    int n;
    public int bits[] = new int[40];
    Scanner inputs = new Scanner(System.in);
    Stuff(int x){
        n=x;
    }
    public void input(){
        for(int i=0 ; i<n ; i++){
            bits[i] = inputs.nextInt();
        }
    }
    public void len_check(){
        int j=0;
        for(int i = 0 ; i< n ; i++){
                j=0;
                while(bits[i+j] == 0 && j<LENGTH){
                    j+=1;
                }
                System.out.println("ABHINAV " + j );
                if(j>5){
                    shift(i+6);
                    bits[i+6] = 1;
                    count+=1;
                    System.out.println(count);
                }
        }
    }
    public void shift(int u){
        for(int i=n ; i>= u ;i--){
            bits[i+1] = bits[i];
        }
    }
    public void display(){
        for(int i=0 ; i<n+count ; i++){
            System.out.println(" " + bits[i]);
        }
    }
}

class Problem{
    public static void main(String args[]){
        int n;
        Scanner inputs = new Scanner(System.in);
        System.out.println("\nEnter bit stream length");
        n = inputs.nextInt();
        Stuff stuff = new Stuff(n);
        System.out.println("Now Enter the bits: ");
        stuff.input();                  // Enter the bit stream
        System.out.println("Entered Bits are:  ");
        stuff.display();
        stuff.len_check();
        System.out.println("Altered Bits are: ");
        stuff.display();
    }
}

最佳答案

这是因为输入只有10个位置长,在第一个10个值之后,数组bits0(标准值)填充。
它按编程方式工作,从位置10开始查找6 0

到达位置n - 5时应停止,例如:

...
int j = 0;
for (int i = 0 ; i< n-5 ; i++) {
    j = 0;
...

10-07 13:11