Looping Through ASCII Art in Java: Solving a Homework Predicament

  • Comp Sci
  • Thread starter Hiche
  • Start date
  • Tags
    Java Loop
In summary, the conversation discusses a homework assignment to print ASCII art using loops and methods. There is also mention of using built-in math methods to increase the number of stars in each line according to a specific sequence. The individual speaking is unsure how to incorporate this sequence into a loop and is struggling to understand the assignment. They also mention that the pattern of increasing stars may not make sense, but it is how the instructor has assigned the task. A potential solution using a counting loop is proposed.
  • #1
Hiche
84
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
  • #2
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.
 
  • #3
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?
 
  • #4
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
}
 
  • #5


Yes, there is a way to use that formula in a loop to print the desired ASCII art. Here is an example of how you can use a for loop in Java to print the art:

// Define the number of rows you want to print
int rows = 5;

// Use a for loop to iterate through each row
for (int i = 0; i < rows; i++) {

// Calculate the number of stars to print using the formula
int stars = 2 + 2 * (i % 2);

// Use a nested for loop to print the appropriate number of spaces and stars
for (int j = 0; j < rows - i - 1; j++) {
System.out.print(" ");
}
for (int k = 0; k < stars; k++) {
System.out.print("*");
}

// Move to the next line
System.out.println();
}

This code will produce the following output:

*
***
*****
*******
*********

You can also use this formula in a while or do-while loop if you prefer. The key is to understand the pattern and use a loop to print the appropriate number of spaces and stars in each row. I hope this helps with your homework predicament. Good luck!
 

1. What is a loop predicament in Java?

A loop predicament in Java is a situation in which a loop, such as a for or while loop, becomes stuck and cannot terminate because the specified condition is never met. This can lead to an infinite loop, causing the program to crash or run indefinitely.

2. What causes a loop predicament in Java?

A loop predicament in Java can be caused by a variety of factors, such as a logic error in the loop condition, incorrect use of loop control statements, or data that does not match the expected format. It can also occur when the loop is not designed to handle all possible scenarios.

3. How can I prevent a loop predicament in Java?

To prevent a loop predicament in Java, it is important to carefully design and test your loops. Make sure that the loop condition is properly defined and that all possible scenarios are accounted for. Additionally, consider using break statements or other loop control statements to ensure that the loop terminates when necessary.

4. How do I fix a loop predicament in Java?

If you encounter a loop predicament in Java, the first step is to identify the cause of the problem. Check your loop condition and make sure it is properly defined. You may also need to review your loop control statements and make sure they are being used correctly. If necessary, you may need to modify your loop logic to handle all possible scenarios.

5. Are there any tools or techniques to help identify loop predicaments in Java?

Yes, there are several tools and techniques that can help identify loop predicaments in Java. One option is to use a debugging tool, which allows you to step through your code and track the values of variables as the program runs. You can also use logging or print statements to track the flow of your program and identify any potential issues. Writing thorough test cases can also help identify loop predicaments by testing different scenarios and inputs.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top