简体   繁体   中英

Error in for loop while multiple initialization

I m trying to do this question: Question

I m getting error in the for loop: for(int i=b,int j=0;i< 2*b, j

My approach is storing all the differences in an array which we get by subtracting the initial value, last value and partition.

What is wrong and what is the alternate way to do this?

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        int a, b;
        Scanner in = new Scanner(System.in);
        a= in.nextInt();
        b= in.nextInt();
        in.nextLine();
        int[] ar = new int[b];

        for(int i=0;i<b;i++)
            {
            ar[i]= in.nextInt();
        }

        int[] ar2 = new int[a];
        // I m trying to get the values which can be obtained from each partition
        for(int i=0;i<b;i++)
            {
            ar2[i] = a - ar[i];
            }



       for(int i=b,int j=0;i< 2*b, j<b; i++, j++)
          {
           ar2[i] = ar[j];
        }


        for(int i=0;i<a;i++)
            {
            System.out.print(ar2[i]+" ");
            }


    }
}

Is this compilation error you're getting? This particular line doesn't follow Java syntax:

for(int i=b,int j=0;i< 2*b, j<b; i++, j++)

For instance, you having 2 int declarations inside the for loop is not possible:

int i=b,int j=0;

This must be re-written to:

int i=b, j=0;

On the conditional part, it is not possible to set multiple conditions separated by comma. What you can do is use logical operators to combine 2 conditional expressions - use either || or &&

i< 2*b, j<b;

This must be rewritten to:

for(int i=b, j=0;i< 2*b && j<b; i++, j++)

Or this:

for(int i=b, j=0;i< 2*b || j<b; i++, j++)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM