Hello all,

Im trying to figure out how to get the for loop

for (int i = 1; i <= (11 - 2 * line); i++) {
    System.out.print("*");

... to show the method listed at the bottom. I've been able to get the rest of it, just not this part. I had the * to show me if I got the pattern right or not.

Thank you.

public class DrawCone {
  public static void main (String[] args) {
    drawPattern();
      for (int line = 5; line >= 1; line --) {
       for (int i = 1; i <= (line - 0); i++) {
        System.out.print("-");
       }
       for (int i = 1; i <= (11 - 2 * line); i++) {
        System.out.print("*");
       }
       for (int i = 1; i <= (line - 0); i++) {
        System.out.print("-");
       }  
      System.out.println();      
    }  
  }        
   public static void printDesign() {
    System.out.println("-----1-----");
    System.out.println("----333----");
    System.out.println("---55555---");
    System.out.println("--7777777--");
    System.out.println("-999999999-");     

  }
}

Recommended Answers

All 2 Replies

Welcome to the forum mate! As will be mentioned to you place all your coding inside the code brackets for readability purpose.

Have you tried out the for loop you have above? Is line in that for loop equal to 5? You haven't specified so lets assume it is. That would mean you are asking this:

i = 1, Is i less than or equal to 1? TRUE,
Print * to screen, Increment i,
i = 2, is i less than or equal to 1? FALSE,
exit loop.

So I don't see how this would help you with the pattern.

You need to break the loop down if you want to find out how to use it. Also are you only allowed to use for loops or can you use an if statement in there too because it would help a-lot.

for (int i = 1; i <= (11 - 2 * line); i++) {
System.out.print("*");

This will print "*", ( 11- (2*line) ) times. So if line == 5 , it'll print "*", if line == 1 it'll print "*********"

So given that your outside loop descends from 5 to 1, this should do what you're looking for - the logic looks correct to me.

What's the output you're seeing? It's easier to figure out how to solve your problem if you tell us what it is.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.