I have this code, which I am trying to print the following shape out...
****
* *
**
*
Current Code:
System.out.print("\nEnter number of rows: ");
rows = kybd.nextInt();
for (int i = 0; i < rows; i++) {
System.out.print("*");
}
for (int i = 0; i < rows; i++) {
for (int j = 1; j <= i; j++) {
if (j == 1 || j == rows - 1 || j == i) {
System.out.print("*");
} else {
System.out.print("0");
}
}
System.out.println();
}
For some reason, it prints out like this:
****
*
**
* *
Any idea how to reverse the second for loop, so it prints the other way?
I feel you should change your i loop
for (int i = rows - 1; i > 0 ; i--)
Try this:
Scanner kybd = new Scanner(System.in);
System.out.print("\nEnter number of rows: ");
int rows = kybd.nextInt();
if (rows > 1) {
for (int i = 0; i < rows; i++)
System.out.print("*");
System.out.println();
for (int i = rows - 1; i > 1; i--) {
System.out.print("*");
for (int j = 2; j < i; j++)
System.out.print(" ");
System.out.println("*");
}
}
System.out.println("*");
Sample output:
Enter number of rows: 6
******
* *
* *
* *
**
*
Notice, that you are printing out the first line of asterisk in a separate cycle, not inside the main one. If you want to follow this road, you need to put the first cycle behind the second one.
for (int i = 0; i < rows; i++) {
for (int j = 1; j <= i; j++) {
if ((j == 1) || (j == (rows - 1)) || (j == i)) {
System.out.print("*");
} else {
System.out.print("0");
}
}
System.out.println();
}
for (int i = 0; i < rows; i++) {
System.out.print("*");
}
Related
i made ms excel example:
Anyway, I made code.
But this code is print only half. Why, why,!
for (int i = 1 ; i <= 5 ; i++) {
for (int j = 1 ; j <= 9 ; j++) {
if ( j < 10 - i ) {
System.out.print(" ");
} else
System.out.print("*");
} System.out.println();
}
Why printed only half.: (
Your condition is wrong, it should be:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 9; j++) {
if (j <= 5 - i || j >= 5 + i) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
System.out.println();
}
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
*
***
*****
*******
*********
*******
*****
***
*
for one of my assignments I am required to print out stars in the shape of a diamond. When trying to print odd numbers, everything works out fine. However, when trying to print even numbered diamonds, the spacing in the middle lines is off, and a diamond is missing. Could someone please point out what I am doing wrong, and provide a possible fix?
public void printStarsDiamond(int d)
{
for (int i = 1; i < d; i = i + 2)
{
for (int j = 0; j < (d - i) / 2; j++)
{
System.out.print(" ");
}
for (int k = 0; k < i; k++)
{
System.out.print("*");
}
System.out.println();
}
for (int i = d; i > 0; i = i - 2)
{
for (int j = 0; j < (d - i) / 2; j++)
{
System.out.print(" ");
}
for (int k = 1; k <= i; k++)
{
System.out.print("*");
}
System.out.println();
}
Desired result, if the user enters 6 (d = 6)
If you notice your code, the second for loop is starting from
i = d in
for (int i = d; i > 0; i = i - 2)
but your first loop is not ending at d, you are doing i < d there which will stop before d, so it will go from 1 to 5 if your d value is 6.
Simply change your second main for loop to start from d-1, here is the code
public void printStarsDiamond(int d)
{
for (int i = 1; i < d; i = i + 2)
{
for (int j = 0; j < (d - i) / 2; j++)
{
System.out.print(" ");
}
for (int k = 0; k < i; k++)
{
System.out.print("*");
}
System.out.println();
}
for (int i = d-1; i > 0; i = i - 2)
{
for (int j = 0; j < (d - i) / 2; j++)
{
System.out.print(" ");
}
for (int k = 1; k <= i; k++)
{
System.out.print("*");
}
System.out.println();
}
The code below will print a diamond pattern using a single loop:
private static void PrintDiamondSingleLoop(int row) {
row++;
int mid = row/2;
int midLeft = mid;
int midRight = mid;
int currentRow = 1;
int currentColumn = 1;
for (int i = 1; i <= row * row; i++)
{
if(i % row == 0)
{
System.out.println("");
if(currentRow < mid)
{
midLeft--;
midRight++;
}else
{
midLeft++;
midRight--;
}
currentColumn = 1;
currentRow++;
}
if (currentColumn >= midLeft && currentColumn <= midRight)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
currentColumn++;
}
}
(This code is taken from an article published on my website: https://programtown.com/how-to-print-diamond-pattern-using-single-for-loop-in-java/.)
I can print a triangle but I'd like to know how to get it to print in the form of an arrow like below and yes it is homework.
for (int i = 1; i < 10; i += 2)
{
for (int k = 0; k < (4 - i / 2); k++)
{
System.out.print(" ");
}
for (int j = 0; j < i; j++)
{
System.out.print("*");
}
System.out.println("");
}
You can use the following code:
public static void main (String[] args)
{
int mid = 10/2; //where 10 is number of lines
for (int i = 0; i < 10; i ++) {
if(i < mid){
for(int j = 0; j < i; j++){
if(j == 0 || j == i-1){
System.out.print("*");
}else{
System.out.print(" ");
}
}
} else{
for(int j = 10 - i; j > 0; j--){
if(j == 10-i || j == 1){
System.out.print("*");
}else{
System.out.print(" ");
}
}
}
System.out.println("");
}
}
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.