answersLogoWhite

0


Best Answer

// Import Library

import javax.swing.*;

//Beginning class Diamond

public class Diamond

{

//Main method

public static voidmain(String[] args)

{

//Declaring Variables

String strRow = null;

double dblRow = 0;

//GUI user input

strRow = JOptionPane.showInputDialog("Please enter an ODD number ");

dblRow = Integer.parseInt(strRow);

//validate a odd number

if ((dblRow % 2)== 0)

//if not an odd number user is advised

JOptionPane.showMessageDialog(null, "The number entered is not an ODD number. Please, try again");

//Building diamond

else

{

//setting up first 1/2 of the diamond - rows

for(int a=1; a<(dblRow/2+.5); a++ )

{

//Assigning empty spaces from right side

for (int b=1; b<(dblRow/2+1.5)-a; b++)

{

System.out.print(" ");

}

//filling out with *

for(int c=0; c<(a*2)-1; c++)

System.out.print("*");

System.out.println();

}

//Creating middle line of the diamond

for(int d=1; d

{

System.out.print("=");

}

System.out.println();

//setting our second 1/2 of the diamond - rows

for(int a=1; a<(dblRow/2+.5); a++ )

{

//Assigning empty spaces from right side in second part of diamond

for(int c=0;c

System.out.print(" ");

//filling out with *

for(int b=1; b

{

System.out.print("*");

}

System.out.println();

}

}

}

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

int longestRow = x;

for(int row=1; row<=longestRow;row++){ //places the symbols for the diamond

int i = (2*row) - 1;

if(i>longestRow) i = 2*(longestRow-row+1) - 1;

for(int column=0 ; column<(longestRow-i)/2 ;column++) System.out.print(" "); //creates the empty spaces for the diamond

for(int column=0 ; column<i ; column++) System.out.print(ch);

for(int column=0 ; column<(longestRow-i)/2 ; column++) System.out.print(" ");

System.out.println(); //prints the diamond

//first you have to define ch for the character you want to use, and also enter the number for the longest row and you should be set

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Draw a diamond with for loop in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you exit in nested loop in java?

You may exit a nested loop in Java using a break with a label for the outer loop.


What are functions in JAVA?

for loop function


When is Iteration used in a Java program?

in a loop


How can you repeatedly execute a code in java script?

1) use for loop 2) do while loop


Which Loop avoids check at every iteration?

All loops available in Java (for, while, do-while) have a loop termination condition that would get executed during every iteration of the loop. Without checking the loop condition the loop cannot be terminated and hence avoiding the loop condition check during iteration is not logic and Java does not do it.


Which loop is also called an exit-condition loop in C programming?

The do while loop is also called an exit condition loop in c, c++, and java.


What is a nested loop in java?

A nested loop is a (inner) loop that appears in the loop body of another (outer) loop. The inner or outer loop can be any type: while, do while, or for. For example, the inner loop can be a while loop while an outer loop can be a for loop.


Write a c program Fibonacci series using for loop in java?

Exactly what do you mean by 'C program in Java'


What are various loops available in java?

Java has three kinds of loops 1. For Loop 2. While Loop 3. Do - While Loop Both For loop and While loop would iterate through a certain lines of code within the loop's limit as long as the loop condition is satisfied. A do while loop would execute the loop once even before checking the condition. So in a do while loop, even if the loop condition is not satisfied the loop would execute once. Example Declarations: for(int i = 0; i &lt; n; i++) { ..... } while (i &lt; n) { ... i++; } do { ... i++; } while (i &lt; n) ;


Can you provide a solution to the diamond-square algorithm using Java and recursion?

Yes. It is possible to provide a solution to the diamond-square algorithm using Java and recursion.


Is it possible to draw in java without using applet?

Yes it is possible to draw in java by using AWT package. or by using javax package.


Write a java code to draw a circle inside in an ellipse?

write a program draw circle and ellipse by using oval methods in java