Comp Sci Looping Through ASCII Art in Java: Solving a Homework Predicament

  • Thread starter Thread starter Hiche
  • Start date Start date
  • Tags Tags
    Java Loop
Click For Summary
The discussion revolves around a homework assignment requiring the printing of ASCII art using loops in Java, specifically a pattern of increasing stars. The pattern increases by 2 and 4 alternately, with the series defined as 3 + (-1)^n or 2 + 2 * (n % 2). Participants express confusion about implementing this logic in a loop, particularly regarding the correct counting of stars per line. A suggested loop structure is provided, which includes a conditional statement to skip certain star counts. The conversation highlights the challenge of adhering to the instructor's specific requirements while trying to achieve the desired output.
Hiche
Messages
82
Reaction score
0
..so, we have been assigned a homework to print a certain ASCII art using only loops and methods. We can also use the built-in math methods such as the exponential one and whatnot.

Basically, we have been asked to print an increasing loop that goes as follows:
Code:
           *
          ***
        *******
       *********
     *************
..and so on, so forth. Notice how the stars increase by 2 then by 4, then by 2 then by 4. The sequence in which the stars are increasing within is 2,4,2,4,2,4,2,4...I managed to find the series for that: 3 + (-1)^n or 2 + 2 * (n % 2) where n >= 1.

Is there a way to use that in a loop? I'm slightly bewildered at the moment as this problem overwhelms my basic understanding on loops.
 
Physics news on Phys.org
Hiche said:
..so, we have been assigned a homework to print a certain ASCII art using only loops and methods. We can also use the built-in math methods such as the exponential one and whatnot.

Basically, we have been asked to print an increasing loop that goes as follows:
Code:
           *
          ***
        *******
       *********
     *************
..and so on, so forth. Notice how the stars increase by 2 then by 4, then by 2 then by 4. The sequence in which the stars are increasing within is 2,4,2,4,2,4,2,4...I managed to find the series for that: 3 + (-1)^n or 2 + 2 * (n % 2) where n >= 1.

Is there a way to use that in a loop? I'm slightly bewildered at the moment as this problem overwhelms my basic understanding on loops.
Are you sure about how the numbers of asterisks in a line increase? It would make more sense to me and would make a nicer pattern if they increased by 2 each line.
 
Unfortunately, this is how our instructor assigned us to do. I tried like everything, but no avail. This is just a fraction of a larger program that prints my country's flag.

Is there a way to do that?
 
The counting loop would look something like this.

Code:
for (int i = 0; i < ROW_LIMIT; i++}
{
   starCount = 2*i + 1;
   // The if block skips 5, 11, 17, 23, etc.
   if (starCount % 3 == 2) 
   {
      continue; 
   }
   // Code to calculate the number of spaces to print, and print starCount asterisks
}
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 12 ·
Replies
12
Views
4K