dimanche 29 mars 2015

How to Make a Diamond Shape Using Asterisks in Java (While Loop)?



I have to make a diamond shape using asterisks-* (shift-8). Compile and run the code and you will see that I have the first 3 lines, I just need the last 2 lines. I am only allowed to use a while loop for this assignment. And please, explain the logic for the whole code.



public class StarWhileLoop {
public static void main (String[] args) {
int row = 0;
while(row<3) {
int space = 2 - row;
while(space !=0) {
space--;
System.out.print(" ");

}
int asterisk = 1 + (row*2);
while(asterisk != 0) {
asterisk--;
System.out.print("*");
}
row++;
System.out.println();
}
while(row<2) {
int space = 2 - row;
while(space !=0) {
space--;
System.out.print(" ");
}
}
}
}



Aucun commentaire:

Enregistrer un commentaire