Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Drawing triangle by using for loops.

  1. #1
    Junior Member
    Join Date
    Feb 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Drawing triangle by using for loops.

    Hi all,

    A few days ago I solved a problem in which I had create the figure shown below:

    Screenshot4.PNGScreenshot4.PNG

    import java.awt.Graphics;
    import java.applet.Applet; 
     
    public class driehoek extends Applet {
     
    	public void paint(Graphics g) {
    		int x, y; 
    		for(y=0; y<10; y++)
    			for(x=0; x<y; x++)
    				g.drawRect(20*x, 20*y, 10, 10); 
    	} 
    }

    However, because I have some difficulty understanding for loops and recursion I tried to create the figure shown below:

    Screenshot5.png

    and this is my code:

     
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.Applet;
     
    public class gekkeDriehoek extends Applet {
     
    	public void paint(Graphics g) 
    	{
    		int x, y; 
     
    		for(y=0; y<10; y++)
    		{
    			for(x = 9; x<y; x--)
    			{
    				g.drawRect(x*20, y*20, 10, 10); 
    			}
    		}
     
    	}
    }

    This code, however, returns a blank screen.
    Does any of you see what my error is?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,401
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: Drawing triangle by using for loops.

    Does any of you see what my error is?
    Look at the second for loop. When is the continue clause ever true?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Or triangle exist
    By xkendzu in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 16th, 2014, 01:28 PM
  2. Replies: 6
    Last Post: March 12th, 2014, 12:43 PM
  3. Building a triangle using strings and loops...program trouble.
    By orbin in forum Loops & Control Statements
    Replies: 2
    Last Post: June 13th, 2012, 03:29 PM
  4. [HELP] TRIANGLE!
    By kramista in forum Loops & Control Statements
    Replies: 10
    Last Post: July 29th, 2010, 12:58 PM
  5. Triangle Question
    By Leeds_Champion in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 29th, 2009, 11:33 PM

Tags for this Thread