8

This is part of my homework. All I need is a bit of advice. I need to write some nested loop constructs, to print the following:

"122333444455555"

"+**+++****+++++"

"--***++++-----******+++++++"

Here is my code to print the first set of symbols

public static void main (String[] args)
{
    int i,j;
    for(i=1;i<6;++i)
    {
        for(j=1;j<i+1;++j)
        {
            System.out.print(i);
        }
    }
}

This works perfectly fine. I'm just having trouble figuring out the second and third set of symbols.
Apologies for my lack of experience, I'm fairly new to Java.

Maroun
  • 94,125
  • 30
  • 188
  • 241
Hustl3r28
  • 211
  • 7
  • 16

4 Answers4

4

One solution is:

final String[] arr = {"*", "+"};

And in your inner loop:

System.out.print(arr[i % 2]);

The % (Modulo) operator is responsible of the switches between * and + symbols:

For even i it'll be *, otherwise it'll be +.


Output: "+**+++****+++++".

(Regarding the second output, I'll not show you the solution, but it's very similar to this one once you understand it).

Maroun
  • 94,125
  • 30
  • 188
  • 241
  • I can't understand.. third sequence has also char '-'. Arr provides just two char.. – Luca Davanzo Dec 04 '13 at 12:05
  • @Velthune This solution only for your first sequence. If you understand the modulo operator, you can have a new array and construct a similar output. – Maroun Dec 04 '13 at 12:06
  • Wasn't clear, for me at least! (: I think the real problem is the third sequence. – Luca Davanzo Dec 04 '13 at 12:08
  • @Velthune It's from `_`, `*` and `+`. `_` is printed twice, then `+` 3 times, then `*` four times.. Almost the same logic as the previous symbols expect that it begins from 2 times the first symbol. – Maroun Dec 04 '13 at 12:10
  • Also I think it's the most "organized" way to do this, it's a good practice to use the `%` operator when patterns are involved. – Maroun Dec 04 '13 at 12:12
2
public static void main(String[] args) throws IOException {

    int i, j;

    for (i = 1; i < 6; ++i) {
        for (j = 1; j < i + 1; ++j) {
            System.out.print(i);
        }
    }

    System.out.println();

    for (i = 1; i < 6; i++) {
        if (i % 2 == 1) {
            for (j = 1; j < i + 1; ++j){
            System.out.print("+");
            }
        } else {
            for (j = 1; j < i + 1; ++j){
                System.out.print("*");
            }
        }
    }

    System.out.println();

    for (i = 2; i < 8; i++) {
        if (i % 3 == 1) {
            for (j = 1; j <= i; ++j){
                System.out.print("+");
            }
        } else if (i % 3 == 2) {
            for (j = 1; j <= i; ++j){
                System.out.print("-");
            }
        } else {
            for (j = 1; j <= i; ++j){
                System.out.print("*");
            }
        }
    }
}

Cycle #1: You have to print out numbers from one to five and each number N has to be printed out N times.

for (i = 1; i < 6; ++i) { // this would set `i` to numbers from 1-5

for (j = 1; j < i + 1; ++j) { // for each cycle (next number) it prints 
//it out N times where N is the cycle number. 1 is the first cycle,
//2 is the second and so on.

Cycle #2: Same problem but instead of printing out number of the cycle you have to print out + or * based on if the cycle number is odd or even.

To check if the number is even you can use:

int number = 1;
if(number % 2 == 0){ // is true if the number is even

This checks whats the remainder from the division of number by two.

Cycle #3: Same as #2 but you start from the second cycle, not from the first and you check for the remainder after division by 3.

Dropout
  • 13,653
  • 10
  • 56
  • 109
  • 1
    I really like how you've broken down each of the codes in your explanation, really makes for easier understanding. Thanks alot. – Hustl3r28 Dec 04 '13 at 12:22
0

If I understand, the third set is composed by sequence of "-*+" so:

String sequence = "-*+";
String s = "+**+++****+++++";
int seqI = 0;
for(i=0; i != s.size(); ++i) {
  for(j=0; j < i+2; ++j) {
    System.out.print(sequence[seqI]);
  }
  if(seqI < sequence.size()) {
    ++seqI;
  } else {
    seqI = 0;
  }  
}
Luca Davanzo
  • 21,000
  • 15
  • 120
  • 146
0

You can define a function like this:

public static char Output(int i, int mode)
{
     if (mode == 1)
     {
        return (char) i;
     }
     else if (mode == 2)
     {
         if (i % 2 == 0)
         {
             return '+';
         }
         else
         {
             return '*';
         }
     }
     else if (mode == 3)
     {
         if (i % 3 == 0)
         {
             return '-';
         }
         else if (i % 3 == 1)
         {
             return '*';
         }
         else
         {
             return '+';
         }
     }
}

And use it just like:

for (int mode = 1 ; mode < 4 ; mode++)
{
    for (int i = 1 ; i < 6 ; i++)
    {
        for (int j = 0 ; j < i + (int)(mode / 3) ; j++)
        {
            System.out.println(Output(i, mode));
        }
    }
}

Note: Yes! Actually my code is hard to read, but if you try to read it, you will learn something more than other answers!