Click here to Skip to main content
15,906,567 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to draw a triangle of asterix sign which is empty inside. But all i could do it to draw it filled. I just want to make it to be empty inside the triangle. I have this so far:
Java
import java.util.Scanner;

public class Triangle {

	public static void main(String[] args) {

		Scanner in = new Scanner(System.in);

		System.out.print("Enter side: ");
		int side = in.nextInt();

		for (int i = 1; i <= side; i++) {
			for (int j = 1; j <= side; j++) {
				if ((i + j) > side) {
					System.out.print("*");
					System.out.print(" ");
				} else {
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	}

}


What I have tried:

I have tried drawing the triangle filled but to make it with space inside is the problem.
Posted
Updated 1-Nov-18 20:52pm
v2
Comments
Sergey Alexandrovich Kryukov 10-May-16 16:04pm    
Of course, because ' ' is an opaque character. What exactly do you want to see through, why? :-)
—SA
Kamal Yusuf 10-May-16 16:06pm    
Okay. So this is my output after executing the code:
*
* *
* * *
* * * *
* * * * *
* * * * * *
As you can see inside the triangle is filled up. But what i want to do is to make the inside empty so there only will be asterix signs on the diagonals. *It shows a right-angles triangle but when its run on the main java console it shows a triangle so pls take it as a triangle not a right-angles triangle*
Patrice T 10-May-16 18:29pm    
Use Improve question to update your question.

Quote:

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

It is nice to draw actual wrong output, it is better to draw the output you want. It is also more informative for us.

Your job is to analyze the problem:
- draw the triangle you want to get.
- write down the sequence of steps (with number of times) to get the result.
- identify repeating patterns.
- write the program
- use the debugger to understand why some parts fails, and probably how to correct them.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

Learn one or more analyse methods, I recommend E.W. Djikstra top-Down analyse method, it is a good start.
https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design[^]
https://en.wikipedia.org/wiki/Structured_programming[^]
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra[^]
https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF[^]
 
Share this answer
 
find the program as expected

import java.util.Scanner;
public class Triangle 
{
	public static void main(String[] args) 
	{
		Scanner in = new Scanner(System.in);
		System.out.print("Enter side: ");
		int side = in.nextInt(); int s, k,i,j;
		for (  i = 1; i < side; i++) 
		{
		    for(s=20; s>=i;s--)
		     System.out.print(" ");
			for (j = 1; j <= i; j++) 
			{
			 if((j==1)|| (j==i))
			 System.out.print(" *");
			 else
			 System.out.print("  ");
		    }
			System.out.print("\n");
		}
		for(s=20; s>=i;s--)
		System.out.print(" ");
		for(k=1;k<=side;k++)
		System.out.print(" *");
	}
}



output i found

Enter side: 10
                     *
                    * *
                   *   *
                  *     *
                 *       *
                *         *
               *           *
              *             *
             *               *
            * * * * * * * * * *
 
Share this answer
 
v2
Comments
CHill60 2-Nov-18 9:11am    
They have probably already failed their course if they've waited 2 years for a solution to their homework :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900