Creating Right-Aligned Triangles with Asterisks - java

I am trying to print right aligned triangles using asterisks. So far I have a program that asks a user for a shape and then a size, then prints out a triangle with those dimensions. So far I have a program for left aligned triangles. How do I make it so it's right aligned? Here is my code so far.
if (shape.equals("triangle")) {
System.out.print("size?");
int size = sc.nextInt();
for (int i = 0; i <= size; i = i + 1) {
for (int j = 0; j < i; j = j+ 1) {
System.out.print("*");
}
System.out.println();
}
...
}

Try adding some padding on the left:
System.out.print("size?");
int size = sc.nextInt();
// input 5
for (int i = 0; i <= size; i = i + 1) {
for (int j = 0; j < size-i; ++j) {
System.out.print(" ");
}
for (int j = 0; j < i; ++j) {
System.out.print("*");
}
System.out.println();
}
*
**
***
****
*****
Demo

Try this, this will add left padding and as result, you'll get a right-aligned triangle:
int star = 1, space = size - 1;
while (n > 0) {
for (int i = 0; i < space; i++) {
System.out.print(" ");
}
for (int i = 0; i < star; i++) {
System.out.print("#");
}
star = star + 1;
space = space - 1;
--size;
System.out.println("");
}

Related

How do I print the same output multiple times, horizontally and vertically, in a grid?

I can print a single pine tree of varying sizes, but how do I print an exact replica of it next to it and underneath it, in a grid, using only for loops? This project utilizes a scanner so that the User can vary the size and number of section each tree has. The last portion of the project allows the user to pick how many trees to print, horizontally(next to each other) and vertically(underneath it). The exact wording and an example: Program makes use of a third input to print a number of trees. Ex: When NUM_TREES is three, a 3x3 grid of trees is printed. Any tips or hints are helpful, thank you.
Here is the code I have to get a single tree:
import java.util.Scanner;
public class PineTreeUpgrade {
static int numSections;
static int sectionSize;
public static void main(String[] args) {
askTheUserForInput();
System.out.println();
System.out.println("Here is your amazing creation! How artistic!");
printTree();
printStem();
}
public static void askTheUserForInput() {
Scanner userInput = new Scanner(System.in);
System.out.println("Hello, user!");
System.out.println("How many section would you like in your pine tree?");
numSections = userInput.nextInt();
System.out.println("Great! How big would you like each section to be?");
sectionSize = userInput.nextInt();
}
public static void printTree() {
for (int k = 0; k < numSections; k++) {
//prints the next section, each one increasing in size
for(int i = 0; i < sectionSize; i++) {
//prints the spaces and number of stars in each section
for (int j = i; j < (sectionSize - 1) + (numSections - 1) - k; j++){
System.out.print(" ");
}
System.out.print("/");
for (int j = 0; j < i + k; j++) {
System.out.print("*");
}
System.out.print("*");
for (int j = 0; j < i + k; j++) {
System.out.print("*");
}
System.out.println("\\");
}
}
}
public static void printStem() {
for(int i = 0; i < 2; i++) {
for (int j = 0; j < (sectionSize - 1) + (numSections -1) + 1; j++) {
System.out.print(" ");
}
System.out.println("|");
}
for (int i = 0; i < 1; i++) {
for (int j = 0; j < (sectionSize - 1) + (numSections -1); j++) {
System.out.print(" ");
}
System.out.print("___");
}
}
}
import java.util.Scanner;
public class PineTreeUpgrade {
static int numSections;
static int sectionSize;
static int gridSize;
public static void main(String[] args) {
askTheUserForInput();
System.out.println();
System.out.println("Here is your amazing creation! How artistic!");
for(int x = 0; x < gridSize; x++){
printTree();
printStem();
System.out.println();
}
}
public static void askTheUserForInput() {
Scanner userInput = new Scanner(System.in);
System.out.println("Hello, user!");
System.out.println("How many section would you like in your pine tree?");
numSections = userInput.nextInt();
System.out.println("Great! How big would you like each section to be?");
sectionSize = userInput.nextInt();
System.out.println("What grid size do you want?");
gridSize = userInput.nextInt();
}
public static void printTree() {
for (int k = 0; k < numSections; k++) {
//prints the next section, each one increasing in size
for(int i = 0; i < sectionSize; i++) {
//prints the spaces and number of stars in each section
for(int x = 0; x < gridSize; x++){
for (int j = i; j < (sectionSize - 1) + (numSections - 1) - k; j++){
System.out.print(" ");
}
System.out.print("/");
for (int j = 0; j < i + k; j++) {
System.out.print("*");
}
System.out.print("*");
for (int j = 0; j < i + k; j++) {
System.out.print("*");
}
System.out.print("\\");
int width = 2 * (sectionSize + numSections - 1) + 1;
if(x != gridSize - 1){
for(int y = sectionSize + numSections + k + i; y < width; y++)
System.out.print(" ");
System.out.print(" ");
}
}
System.out.println();
}
}
}
public static void printStem() {
for(int i = 0; i < 2; i++) {
for(int x = 0; x < gridSize; x++){
for (int j = 0; j < (sectionSize - 1) + (numSections -1) + 1; j++)
System.out.print(" ");
System.out.print("|");
if(x != gridSize - 1)
for (int j = 0; j <= sectionSize + numSections; j++)
System.out.print(" ");
}
System.out.println();
}
for (int i = 0; i < 1; i++) {
for(int x = 0; x < gridSize; x++){
for (int j = 0; j < (sectionSize - 1) + (numSections -1); j++)
System.out.print(" ");
System.out.print("___");
if(x != gridSize - 1)
for (int j = 0; j < sectionSize + numSections; j++)
System.out.print(" ");
}
}
}
}
width_of_double_pine_tree = 2 * width_of_single_pine_tree + buffer_between_trees
For loops don't have to define a variable, they can use an existing one.
int j = 0;
for(; j < (width_of_double_pine_tree - (width_of_single_pine_tree + buffer_between_trees)); j++)
//this is your first tree
for(; j < (width_of_double_pine_tree - width_of_single_pine_tree); j++)
//this is your buffer (S.o.p spaces)
for(; j < (width_of_double_pine_tree); j++)
//this is your second tree
Your canvas is always a rectangle, you subtract from it, the edge spaces you aren't using, void space with spaces (tabs ruin everything, because they aren't uniform, use spaces in everything!)
Use your i variable to keep track of the height that you're currently painting on your canvas so you can math in your pine needles and trunk. For the second tree, in the math portion, offset your j by the buffer_between_trees and width_of_single_tree if(i < (j + buffer + width)), etc.
Make sure you do the assignment before you tackle extra stuff.

How do i draw an Arrow in Java?

Here is what I am trying to create:
*
**
***
****
*********
****
***
**
*
Here is what i have created:
*
**
***
****
**********
****
***
**
*
Here is my code:
for(int i = 1; i <= 5; i++){
for(int j = 1; j<=i; j++){
System.out.print("*");
}
System.out.println();
if(i == 4){
for(int f = 0; f < 5; f++){
System.out.print("*");
}
}
}
for(int i = 1; i <= 5; i++){
for(int j = 4; j>=i; j--){
System.out.print("*");
}
System.out.println();
}
I dont know how to indent the tail part, please dont give me the answer, just tell me where the problem is and I will try to do it. Thank you!
Given the shape you have provided, and assuming it's going to be output in a monospace font, we can draw it on a grid.
Splitting it visually into 2 sections, we can see that there are 2 main modes.
The tail (green), and the head (red).
There's also a second mode. where the number of stars in the head increase and decrease.
The size of the tail could potentially vary, as could the size of the head, and it would still be a shape we recognise as an arrow.
When outputting text, the easiest iteration order is generally left to right, up to down, unless dealing with right to left languages, or vertical reading, I'm going to assume western culture for the output, as that is what's been popularised in programming output streams.
So, the question is, how best to build up the strings required for output.
Given the format that the output is going to be on a stream,
width is going to be your outside loop, and tail/length your inside loop.
The code you provided uses 10 for width, by splitting it into 2 groups of 5, with the second one being offset by one.
for(int i = 1; i <= 5; i++){
for(int j = 1; j<=i; j++){
System.out.print("*");
}
System.out.println();
if(i == 4){
for(int f = 0; f < 5; f++){
System.out.print("*");
}
}
}
for(int i = 1; i <= 5; i++){
for(int j = 4; j>=i; j--){
System.out.print("*");
}
System.out.println();
}
so translating your code back to the drawings, you loop over width, and on the last iteration output the line of stars for the tail.
What you need to do, is output a space on all other lines.
There are a variety of ways to do so, you could output the character, or use one of the fixed width formatting functions using printf.
But given your current code, the minimal difference will be to output a space character, when the width iteration for the increasing mode is not 4, and for the loop of width - 1.
Looking at your end result, your tail is being printed after your head. That needs to move earlier.
Your tail is too long, need to offset that by 1.
And you need to insert pink and brown sections by printing spaces, the same amount of times as you output the tail, whenever you do not output the tail.
You should add another inner for-loop that prints " " before the for-loop that prints "*".
You can use printf method to tell Java print in specific format. For example, you need 5 spaces prefixed then print your text:
System.out.printf("%5s","hello");
To get your desired output, try the below code. Note: There may be better solutions.
for (int i = 1; i <= 5; i++) {
if (i != 5) // middle line should not be prefix with spaces
// empty 5 spaces before starting the loop
System.out.printf("%5s", "");
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
if (i == 4) {
for (int f = 0; f < 5; f++) {
System.out.print("*");
}
}
}
for (int i = 1; i <= 5; i++) {
// print 5 space before
System.out.printf("%5s", ""); // <== note the printf here with empty string
for (int j = 4; j >= i; j--) {
System.out.print("*");
}
System.out.println();
}
Here's a method you can use to print the arrow:
private static void printArrow (final int min, final int max, final int tip)
{
final int numSpaces = tip - max - 1;
// print the top
for (int i = min; i <= max; i++)
{
for (int j = 0; j < numSpaces; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.println();
}
// print the tip
for (int i = 0; i < tip; i++)
{
System.out.print("*");
}
System.out.println();
// print the bottom
for (int i = max; i >= min; i--)
{
for (int j = 0; j < numSpaces; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.println();
}
}
Call it from main with:
public static void main(String[] args)
{
// this will print the arrow in your question
printArrow(1, 4, 9);
}
Another Example:
printArrow(1, 10, 19);
Outputs:
*
**
***
****
*****
******
*******
********
*********
**********
*******************
**********
*********
********
*******
******
*****
****
***
**
*
Here is my answer. You just need to first insert the space and then print * until you reach the middle line. If you want to make it dynamic, just replace the boundary value for k dynamically the same as the boundary for the f:
public static void main(String[] args) {
int k = 1;
int m = 0;
for (int i = 1; i <= 5; i++) {
m = i - 1;
while (k != 6 && m != 4) {
System.out.print(" ");
k++;
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
if (i == 4) {
for (int f = 0; f < 5; f++) {
System.out.print("*");
}
}
k = 1;
}
k = 1;
for (int i = 1; i <= 5; i++) {
while (k != 6) {
System.out.print(" ");
k++;
}
for (int j = 4; j >= i; j--) {
System.out.print("*");
}
System.out.println();
k = 1;
}
}
EDIT
I realized the output is one* behind. So just need to change the boundary value of k from 5 to 6 (from the value of boundary for f to the value of boundary for f+1)
The output looks like this:
for (int i = 1; i <= 5; i++) {
if (i!=5) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
if (i == 4) {
for (int f = 0; f < 5; f++) {
System.out.print("*");
}
}
}
for (int i = 1; i <= 5; i++) {
System.out.print(" ");
for (int j = 4; j >= i; j--) {
System.out.print("*");
}
System.out.println();
}

Java - Hourglass

I'm missing by just a little bit. What I want:
*******
*****
***
*
***
*****
*******
What I'm getting
*******
*****
***
*
*
***
*****
*******
The code
public class HD404 {
public static void main(String[] args) {
int N = StdIn.readInt();
int x = N*2-1;
for (int i = 0; i < N; i++) {
for (int j = i; j > 0; j--) {
StdOut.print(" ");
}
for (int k = 0; k < x; k++) {
StdOut.print("*");
}
x-=2;
StdOut.println();
}
x = 1;
for (int i = 0; i < N; i++) {
for (int j = i; j < N-1; j++) {
StdOut.print(" ");
}
for (int k = 0; k < x; k++) {
StdOut.print("*");
}
x += 2;
StdOut.println();
}
}
}
Right now I'm mostly just guessing and I just can't pin point my error. What am I missing here?
The problems lays with the second part of your code where you ask to draw one star and you start at zero where you should start at one.
Solution
x = 1;
for (int i = 0; i < N; i++)
should be replaced with
x = 3;
for (int i = 1; i < N; i++)
The problem is that you are starting to draw the bottom of the hourglass with 1 asterisk (x = 1) instead of 3.
The second issue is that the bottom of the hourglass only has N-2 lines, not N-1 so the loop should start at 1 instead of 0. This is because the line with a single asterisk was already drawn in the upper-half.
Corrected code:
public static void main(String[] args) {
int N = StdIn.readInt();
int x = N*2-1;
for (int i = 0; i < N; i++) {
for (int j = i; j > 0; j--) {
StdOut.print(" ");
}
for (int k = 0; k < x; k++) {
StdOut.print("*");
}
x-=2;
StdOut.println();
}
x = 3; // <-- not 1 here, the first line has 3 asterisks
for (int i = 1; i < N; i++) { // <-- i starts at 1 because the first line was already drawn in the upper half
for (int j = i; j < N-1; j++) {
StdOut.print(" ");
}
for (int k = 0; k < x; k++) {
StdOut.print("*");
}
x += 2;
StdOut.println();
}
}
As a side-note, you could rewrite this code a lot shorter by making the following observations:
There are x lines to draw so we can loop from 0 to x included (to respect the symmetry) and skip the middle line so as not to draw it twice
For every line, there are x columns to draw and it is either a space or a *.
For every line, * is drawn only if the current column is between min(i, x-i) and max(i, x-i) (if we're in the upper-part, i < x-i and if we're in the bottom-part, i > x-i).
Code:
public static void main(String[] args) {
int N = 4;
int x = 2 * N - 1;
for (int i = 0; i <= x; i++) {
if (i == N) continue; // skip the middle-line for it not to be drawn twice
for (int j = 0; j < x; j++) {
System.out.print(j >= Math.min(i, x-i) && j < Math.max(i, x-i) ? "*" : " ");
}
System.out.println();
}
}
Sample output:
*******
*****
***
*
***
*****
*******
The easiest way I can think up is probably to prevent the last iteration of your first outer loop, that way you'll prevent the first single star line to be shown.
I would probably do it this way:
for(int i = 0; i < N && x > 1; i++)
{
/*Code of the first inner loop*/
}
For those whose still looking for a simpler and lesser code regarding hourglass challenge. This contains 2 for loops only.
You may use this as reference.
public static void hourGlass(int size) {
// 2 for loops only
int dimension = (size * 2) - 1, space = 0, stars = size - 1, printed = 0;
for(int i=0; i<dimension; i++) {
int actual = space;
for (int j=dimension; j > 0; j--) {
if(actual > 0) {
System.out.print(" ");
actual--;
}
else {
System.out.print("*");
if(stars==printed) {
actual = space;
printed = 0;
} else {
actual = 1;
printed++;
}
}
}
if(i <= size-2) { // will pattern spaces and stars from top to middle
space++;
stars--;
}
else { // will pattern spaces and stars from middle to top
space--;
stars++;
}
System.out.println();
}
}

Using for loops to create an upside-down triangle [duplicate]

This question already has answers here:
Creating a triangle with for loops
(13 answers)
Closed 8 years ago.
Im a java student, and i'm have quite the hard time, trying to use for loops to create an upside down triangle.
This is what my code looks like now, a straight forward triangle. How can i make another one just like it, but upside down?
for (int i=1; i<20; i += 2)
{
for (int k=10; k < (0 - i / 2); k++)
{
System.out.print(" ");
}
for (int j=0; j<i; j++)
{
System.out.print("*");
}
System.out.println("");
}
Is that tricky?
Just change
for (int i=1; i<20; i += 2)
To
for (int i = 19; i >0; i -= 2)
Code.
for (int i = 19; i > 0; i -= 2) {
for (int k = 10; k < (0 - i / 2); k++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println("");
}
Out put:
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
int c = 2*lines;
for (int i = lines-1; i>=0; i--)
{
for (int j = i; j < lines; j++)
{
System.out.print(" ");
}
for (int k = 1; k <= c; k++)
{
if (k % 2 == 0)
{
System.out.print(" ");
}
else
{
System.out.print(symbol);
}
}
System.out.print("\n");
c -= 2;
}
All you have to do is to change your 3rd for loop
for (int j=0; j<i; j++)
{
System.out.print("*");
}
In your code, you print 1 star, 3 stars, 5 stars and so on... (i stars actually)
To make it upside down, start the j at the max value and decrement it so you print n - i stars
for (int j = 20 - i; j > 0; j--)
{
System.out.print("*");
}
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
for (int i = 1; i < 20; i += 2) {
for (int k = 10; k < (0 - i / 2); k++) {
sb.append(" ");
}
for (int j = 0; j < i; j++) {
sb.append("*");
}
sb.append("\n");
}
System.out.println(sb.reverse());
}
However you are probably supposed to learn something about loops and the algorithm.

Printing a triangle in Java

I'm practicing basic coding exercises and trying to print the following triangle in Java:
*
***
*****
***
*
The following code gives me the results but I feel like there must be a much more elegant solution
for (int i = 1; i <= 5; i++) {
if (i % 2 == 1) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println("");
}
}
for (int i = 3; i > 0; i--) {
if (i % 2 == 1) {
for (int j = 1; j < i + 1; j++) {
System.out.print("*");
}
System.out.println("");
}
}
Can anyone provide some insight into how to make this work in a better way?
Ok, here's some more code that produces the correct result that uses just the two for loops, but it looks even uglier:
for (int i = 1; i <= 10; i += 2) {
if (i <= 5) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println("");
}
else if(i > 5 && i < 8){
for(int j = i/2; j > 0; j--){
System.out.print("*");
}
System.out.println("");
}
else{
for(int j = 1; j > 0; j--){
System.out.print("*");
}
System.out.println("");
}
}
First, you are skipping each 2nd iteration of the loop because you want to increase two steps at once. You can do this by changing the "i++" in your loop to "i += 2" and "i--" to "i -= 2", that will have the same effect and allows you to remove the if inside both loops.
Another improvement would be using a single outer loop and figuring out whether the inner loop should be increasing or decreasing the amount of asterisks. Maybe you can come up with an equation that gives you the amount of asterisks based on the value of i? (I didn't want to solve it completely so you have some exercise left, just comment if you want a full solution)
Updated with a solution that might be considered elegant as you can change the height of the triangle and there is no repetition:
int height = 5;
for (int i = 1; i <= 2 * height; i += 2) {
int numAsterisks;
if (i <= height) {
numAsterisks = i;
} else {
numAsterisks = 2 * height - i;
}
for (int j = 0; j < numAsterisks; j++) {
System.out.print("*");
}
System.out.println();
}
What about the following?
public void printTriangle(int size) {
int half = size / 2;
for (int i = 0; i < size; i++) {
int stars = 1 + 2 * (i <= half ? i : size - 1 - i);
char[] a = new char[stars];
Arrays.fill(a, '*');
System.out.println(new String(a));
}
}
Or just a bit more optimized:
public void printTriangle(int size) {
int half = size / 2;
char[] a = new char[size];
Arrays.fill(a, '*');
for (int i = 0; i < size; i++) {
int stars = 1 + 2 * (i <= half ? i : size - 1 - i);
System.out.println(new String(a, 0, stars));
}
}
for(int i = 0; i < 7; i++) {
for(int j = 0; j < i; j++) {
print("*");
}
print("\n");
}
This can be another solution to print a regular right triangle...
Here's a different way of looking at the problem. By using an integer array, I can solve lots of shape drawing problems by changing the values in the array.
When solving more difficult problems, you would use model classes instead of simple integers. The idea, however, is the same.
Here's the output.
*
***
*****
***
*
And here's the code:
public class Triangle {
public static void main(String[] args) {
int[] heights = {1, 3, 5, 3, 1};
for (int i = 0; i < heights.length; i++) {
for (int j = 0; j < heights[i]; j++) {
System.out.print("*");
}
System.out.println("");
}
}
}
How about...
int width = 5;
for (int i = 1; i <= width; i+=2){
System.out.println(String.format("%"+i+"s", "").replaceAll(" ", "*"));
}
for (int i = width-2; i > 0; i-=2){
System.out.println(String.format("%"+i+"s", "").replaceAll(" ", "*"));
}
Or, even better yet...
int width = 7;
double half = width / 2
for (int i = 0; i < width; i++){
System.out.println(String.format("%"+((i < half ? i : (width-i-1))*2+1)+"s", "").replaceAll(" ", "*"));
}
Gives
*
***
*****
***
*

Categories