English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am trying to write a program for my online Java programming class. It is requiring me to write a program that displays a diamond using asterisks. I can get the nested for statements to display the number of asterisks correctly but I cannot manage to figure out how to insert the spaces so that it looks like a diamond (i.e. the first one should be 5 spaces from the left). Help is greatly appreciated.

2007-02-21 05:12:14 · 5 answers · asked by Anonymous in Computers & Internet Programming & Design

5 answers

i will assume the program has an odd number of lines
so let's say the number of lines is m.
declare a variable k before the looping that equals (m-1)/2
now in the nested loop (the one the types the stars) make another loop that iterates Math.abs(k) times, printing a space each time...
now after the loop that prints the stars (and now a has another loop inside) type k--;...
now if u want a diamond with 11 lines, k will equal 5, which is th number of sapces in the first line, then four,...when it reaches 0, this is ur middle line (the one with no spaces) then k becomes -1, -2,-3...but since we used Math.abs, it goes back to 5 spaces in the last line...
hope u find this helpful

2007-02-21 05:38:59 · answer #1 · answered by Khaled Z 3 · 0 0

The HTML code for a "space" is  .

Edit:
Haha. Yahoo! put a space on my code. Anyway, the HTML code for a "space" is: & n b s p. Remove the spaces in between. So say you need 5 spaces from left, type those 5 times. ^^

2007-02-21 05:17:05 · answer #2 · answered by WildGod 2 · 0 0

Why roll your person whilst there is in all hazard a calendar widget you could reuse? by using the way, the Java API itself delivers many smart training and interfaces: Date, Calendar, GregorianCalendar, DateFormat, and SimpleDateFormat.

2016-12-18 08:02:19 · answer #3 · answered by ? 3 · 0 0

Hi..run the following program..this will display a diamond using *.
You can change the size(change 30 to anything) also the * to anything(change design variable)..Then you can modify this according to your requirement.

public class Testing1 {

public static void main(String[] args){
Testing1 t = new Testing1();
t.execute();
}

public String getSpace(int n){
String space = "";
for(int j=0;j<=n;j++){
space = space + " ";
}
return space;
}


public String getlead(int n, int len){
String space = "";
for(int j=len;j>=n;j--){
space = space + " ";
}
return space;
}

public void execute(){
Testing1 t = new Testing1();
String design = "=";
for(int i=0;i<30;i++){

String space = t.getSpace(i);
String lead = t.getlead(i,30);
System.out.println(lead + design + space + space +design);
}

for(int i=30;i>=0;i--){

String space = t.getSpace(i);
String lead = t.getlead(i,30);
System.out.println(lead + design + space + space+ design);
}
}
}

2007-02-21 11:03:36 · answer #4 · answered by ravikumar_g26 2 · 0 0

its a space

2007-02-21 05:22:11 · answer #5 · answered by "G" 1 · 0 0

fedest.com, questions and answers