Drawing a Diamond with User Input (not double of the user input) - java

So I've been trying to draw a Diamond shape in Java and I've got the top part of the diamond done but the bottom part is not printing as I want it to. Instead of decreasing towards the end, it stays the same or it increases as it go down.
import java.util.Scanner;
public class Q1_Diamond{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("How many lines? ");
int lines = sc.nextInt();
// Top half of Diamond
for(int i = 0; i < lines/2; i++){
for(int j = 0; j < (lines-1)-i; j++){
System.out.print(" ");
}
for(int j = 0; j < i; j++){
System.out.print("*");
}
for(int j = 0; j < i+1; j++){
System.out.print("*");
}
System.out.println();
}
// Bottom half of Diamond
// Even number of lines
if(lines % 2 == 0){
for(int k = 0; k < (lines/2); k++){
for(int j = 0; j < (lines/2)+k; j++){
System.out.print(" ");
}
for(int j = 0; j < (lines/3 - 0.5f); j++){
System.out.print("*");
}
for(int j = 0; j < lines/2+1; j++){
System.out.print("*");
}
System.out.println();
}
}
// Odd number of lines
else{
not done yet...
}
}
}

Remove the if condition (i.e. if(lines % 2 == 0)) for the lower half and simply repeat the same code as the upper half with the following loop declaration:
for (int i = lines % 2 == 0 ? lines / 2 - 1 : lines / 2; i >= 0; i--)
Complete code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many lines? ");
int lines = sc.nextInt();
// Top half of Diamond
for (int i = 0; i < lines / 2; i++) {
for (int j = 0; j < (lines - 1) - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
for (int j = 0; j < i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
// Bottom half of Diamond
for (int i = lines % 2 == 0 ? lines / 2 - 1 : lines / 2; i >= 0; i--) {
for (int j = 0; j < (lines - 1) - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
for (int j = 0; j < i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
A sample run:
How many lines? 10
*
***
*****
*******
*********
*********
*******
*****
***
*
Another sample run:
How many lines? 11
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
[Update]
You can extract the common code into a method e.g. printLine as shown below:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many lines? ");
int lines = sc.nextInt();
// Top half of Diamond
for (int i = 0; i < lines / 2; i++) {
printLine(lines, i);
}
// Bottom half of Diamond
for (int i = lines % 2 == 0 ? lines / 2 - 1 : lines / 2; i >= 0; i--) {
printLine(lines, i);
}
}
static void printLine(int lines, int i) {
for (int j = 0; j < (lines - 1) - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
for (int j = 0; j < i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}

Another way to tackle this is to determine expressions for the number of spaces and asterisks in each line of the star. Obviously this will need to be in terms of the number of lines (n) and the line we're on.
If we number lines from i = 0 to n-1 we get
nAsterisks = 1 + 2 * min(i, n-i-1)
and
nSpaces = (n+1)/2 - 1 - min(i, n-i-1)
With these we can write some compact Java:
static void printStar(int n)
{
for(int i=0, k=0; i<n; i++, k=Math.min(i, n-i-1))
{
for(int j=0; j<(n+1)/2-1-k; j++) System.out.print(" ");
for(int j=0; j<1+2*k; j++) System.out.print("*");
System.out.println();
}
}
For printStar(8) we get:
*
***
*****
*******
*******
*****
***
*
and printStar(9) gives
*
***
*****
*******
*********
*******
*****
***
*

Related

Creating Right-Aligned Triangles with Asterisks

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("");
}

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.

I need help writing a program that prints out two shapes on one line using nested loops

Here is what the shapes should look like :
Here is my code so far:
public class Diamonds {
public static void main(String[] args) {
for (int i = 1; i < 10; i += 2) {
for (int j = 0; j < 9 - i / 2; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.print("\n");
}
for (int i = 7; i > 0; i -= 2) {
for (int j = 0; j < 9 - i / 2; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.print("\n");
}
}
}
I am having trouble getting the second shape
In order to not spoil your fun with this problem, I will explain what you need to do without writing any code.
To get the second shape in there, you would need to add two additional nested for loops into each of the two "outer" loops that you already have.
Loops number three will produce a fixed number of spaces. Note that the distance between the right edge of the first shape and the left edge of the second shape is constant, so your third loops will be easy to code up.
Loops number four will loop like your first loop, but they would change places: the first inner loop from the first outer loop will be the forth inner loop in the second outer loop, and vice versa.
By examining the shape on the right, we can notice that for each N asterisks on the line in the left shape, the right one has 10 - N, so, taking your code and extending it, we can get:
public class Diamonds {
public static final String SPACE = " ";
public static void main(String[] args) {
for (int i = 1; i < 10; i += 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.print(SPACE);
for (int j = 0; j < 10 - i; j++)
System.out.print("*");
System.out.print("\n");
}
for (int i = 7; i > 0; i -= 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.print(SPACE);
for (int j = 0; j < 10 - i; j++)
System.out.print("*");
System.out.print("\n");
}
}
}
And if we extract some common code:
public class Diamonds {
public static final String SPACE = " ";
public static void main(String[] args) {
for (int i = 1; i < 10; i += 2) {
drawLine(i);
System.out.print("\n");
}
for (int i = 7; i > 0; i -= 2) {
drawLine(i);
System.out.print("\n");
}
}
private static void drawLine(int i) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.print(SPACE);
for (int j = 0; j < 10 - i; j++)
System.out.print("*");
}
}
public class ReverseDiamond {
public static void main(String[] ar) {
int rows = 10;
ReverseDiamond diamond = new ReverseDiamond();
for(int i = 0; i < rows; i++)
diamond.printLine(rows, i);
for(int i = rows - 2; i >= 0; i--)
diamond.printLine(rows, i);
}
private void printLine(int rows, int currRow) {
for(int space = 1; space <= currRow; space++)
System.out.print(" ");
for(int star = 1; star < 2 * (rows - currRow); star++)
System.out.print("*");
System.out.println();
}
}
You may like that :
public class Diamonds {
public static void main(String[] args) {
int totalStars = 9;
int rows = 9;
for (int r = 0,stars=-1,gap=totalStars; r < rows; r++ ) {
stars+= (r<=rows/2) ?2:-2;
gap=totalStars-stars;
printChars(' ', gap);
printChars('*', stars);
printChars(' ', gap);
int gap2=stars+1;
int stars2=gap+1;
printChars(' ', gap2);
printChars('*', stars2);
printChars(' ', gap2);
System.out.println();
}
}
private static void printChars(char c ,int times) {
for (int i = 0; i < times; i++) {
System.out.print(c);
}
}
}
try this :
public static void main(String[] args) {
for (int i = 9; i > 0; i -= 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.print("\n");
}
for (int i = 2; i < 10; i += 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j <= i; j++)
System.out.print("*");
System.out.print("\n");
}
}
output :
*********
*******
*****
***
*
***
*****
*******
*********

Categories