简体   繁体   中英

Set if-condition for new line every 10th character

I'd like to enter a new line after the 10th array. My array is filled from 1-365 (day[0]=1, day[1]=2, ..) .

This is my code and i stuck at creating the if-condition :

int day[] = new int[365];

        for(int i=0; i<day.length; i++){
            day[i] = i+1;
            System.out.print(day[i]+" ");
            if((i % 10) == 0){
                System.out.println();
            }
        }

This should be my console output:

1 2 3 4 5 6 7 8 9 10 
11 12 13 14 15 16 17 18 19 20 
21 22 23 24 25 26 27 28 29 30 
31 32 33 34 35 36 37 38 39 40 
41 42 43 44 45 46 47 48 49 50 
51 ...

What I'm getting:

1 
2 3 4 5 6 7 8 9 10 11 
12 13 14 15 16 17 18 19 20 21 
22 23 24 25 26 27 28 29 30 31 
32 33 34 35 36 37 38 39 40 41 
42 43 44 45 46 47 48 49 50 51 

Change your condition to :

        if(((i+1) % 10) == 0){
            System.out.println();
        }

You are printing i+1 , so you want to add a line break when i+1 is divisible by 10.

You can also skip using the array and print like this:

public static void main(String[] args) {
    for (int i = 0; i < 365; ++i) {
        System.out.format("%3d ", i + 1);
        if ((i + 1) % 10 == 0)
            System.out.println();
    }
} 

Output

  1   2   3   4   5   6   7   8   9  10 
 11  12  13  14  15  16  17  18  19  20 
 21  22  23  24  25  26  27  28  29  30 
 31  32  33  34  35  36  37  38  39  40 
 41  42  43  44  45  46  47  48  49  50 
 51  52  53  54  55  56  57  58  59  60 
 61  62  63  64  65  66  67  68  69  70 
 71  72  73  74  75  76  77  78  79  80 
 81  82  83  84  85  86  87  88  89  90 
 91  92  93  94  95  96  97  98  99 100 
101 102 103 104 105 106 107 108 109 110 
....

This is because the array index starts at 0. So what you consider the 10th character is, to Java, the 9th (the first being the 0th).

Check ((i + 1) % 10) instead.

Alternatively you could check the actual value of day[i] , since you've already set that to i + 1 .

This would be your solution for example: If you don't understand it, make it much more easier for your example:

public static void main(String[] args){
    System.out.println("This would be your solution:");
    int day[] = new int[365];
    for (int i = 0; i < 365; i++) {
        day[i]=i+1;
        //System.out.print(day[i]);
    }

    for (int i = 0; i < day.length; i++) {
        int j = day[i];
        System.out.print(j+" ");

        if(j%10==0){
            System.out.println();
        }
    }

}

try this

 int day[] = new int[365];

    for(int i=1; i<day.length; i++){
        day[i-1] = i;
        System.out.print(day[i-1]+" ");
        if((i % 10) == 0){
            System.out.println();
        }
        if(i==day.length-1){
            day[i] = i+1;
            System.out.print(day[i]+" ");
        }
    }

OUTPUT

1 2 3 4 5 6 7 8 9 10 

11 12 13 14 15 16 17 18 19 20 

21 22 23 24 25 26 27 28 29 30 

31 32 33 34 35 36 37 38 39 40 

41 42 43 44 45 46 47 48 49 50 
....... 

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